首页 > 解决方案 > 模糊查询没有给出任何结果

问题描述

弹性搜索中的模糊查询无法正常工作,即使使用确切的值,结果也是空的。

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}
    }
  }
}

我之前没有使用过模糊查询,根据我的理解,它应该可以正常工作。

标签: elasticsearchfuzzy-searchelasticsearch-query

解决方案


模糊查询不会被分析,但该字段是这样的,因此您的搜索 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"
          ]
        }
      }
    ]

推荐阅读