elasticsearch - 模糊查询没有给出任何结果
问题描述
弹性搜索中的模糊查询无法正常工作,即使使用确切的值,结果也是空的。
ES 版本: 7.6.2
索引映射:以下是映射详细信息
{
"movies" : {
"mappings" : {
"properties" : {
"genre" : {
"type" : "text",
"fields" : {
"field" : {
"type" : "keyword"
}
}
},
"id" : {
"type" : "long"
},
"rating" : {
"type" : "double"
},
"title" : {
"type" : "text"
}
}
}
}
}
文档:索引中存在以下文档
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 2,
"title" : "Raju Ban gaya gentleman",
"rating" : 2,
"genre" : [
"Drama"
]
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"title" : "Baat ban jaegi gentleman",
"rating" : 4,
"genre" : [
"Drama"
]
}
}
]
}
}
查询:以下是我用于搜索文档的查询
GET movies/_search
{
"query": {
"fuzzy": {
"title": {"value": "Bat ban jaegi gentleman", "fuzziness": 1}
}
}
}
我之前没有使用过模糊查询,根据我的理解,它应该可以正常工作。
解决方案
模糊查询不会被分析,但该字段是这样的,因此您的搜索
Bat ban jaegi gentleman
将分为不同的术语并Bat
进行分析,该术语将进一步用于过滤结果。
您也可以参考这个答案以及ElasticSearch 的模糊查询,了解为什么模糊查询在字段上进行分析。
但是由于您要分析完整的标题,您也可以将映射更改title
为拥有keyword
字段。
您可以通过分析 API 了解您的字符串将如何被标记: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-analyze.html
以下是相同的映射:
"mappings": {
"properties": {
"genre": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
}
},
"id": {
"type": "long"
},
"rating": {
"type": "double"
},
"title": {
"type": "text",
"fields": {
"field": {
"type": "keyword"
}
}
}
}
}
现在,如果您在 title.field 上搜索,您将获得所需的结果。搜索查询是:
{
"query": {
"fuzzy": {
"title.field": {"value": "Bat ban jaegi gentleman", "fuzziness": 1}
}
}
}
在这种情况下获得的结果是:
"hits": [
{
"_index": "ftestmovies",
"_type": "_doc",
"_id": "2",
"_score": 0.9381845,
"_source": {
"title": "Baat ban jaegi gentleman",
"rating": 4,
"genre": [
"Drama"
]
}
}
]
推荐阅读
- sockets - 查找服务器是否连接到客户端
- python - 词频+可视化+python
- python - 每个 api 网关 http 代码的集成 lambda 函数或拆分 lambda
- python-docx - 使用 python-docx 和 docxtpl 将图像添加到标题
- express-graphql - GraphQL 查询返回 null
- html - PDF 文件合并:删除页尾的空白。我正在使用 PDFBox v2.0.13 来实现这一点
- arrays - 将数据保存在 json 文件中而不覆盖 python 中的现有文件
- angularjs - 如何将新值绑定到从一个组件到另一个组件的数组变量以及第一个组件中的现有数据
- deployment - Travis CI - 每个部署提供程序的条件“before_deploy”脚本
- mysql - 删除大表上的查询需要很长时间且超出错误锁定等待超时