模糊查询没有给出任何结果
Fuzzy query not giving any results
elastic search 中的模糊查询不起作用,即使使用精确值结果也是空的。
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"
]
}
}
]
elastic search 中的模糊查询不起作用,即使使用精确值结果也是空的。
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"
]
}
}
]