弹性查询中的模糊查询无法正常工作,即使精确值也为空。
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's Fuzzy Query。>
但是由于您要分析完整的标题,因此可以将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"
]
}
}
]