【问题标题】:Fuzzy query not giving any results模糊查询没有给出任何结果
【发布时间】:2020-04-02 10:34:56
【问题描述】:

弹性搜索中的模糊查询不起作用,即使使用确切的值,结果也是空的。

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

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

【问题讨论】:

    标签: elasticsearch fuzzy-search elasticsearch-query


    【解决方案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"
              ]
            }
          }
        ]
    

    【讨论】:

    • 感谢 Prerna 的回答。
    • 我仍然不确定为什么查询不会导致异常。如果 ES 不支持对“文本”字段的模糊搜索,则应触发异常。
    • ES 将文本字段分析为单个标记,因为模糊输出不是您所期望的。这在这篇文章elasticsearch.org/guide/en/elasticsearch/reference/current/… 中有更详细的解释。你可以参考。
    • 为了便于理解,因为您的标题字段是“文本”,所以它被划分为不同的标记。假设对于“Baat ban jaegigentleman”,它将被标记为 Baat、ban、jaegi、绅士。所以,现在如果你通过模糊搜索搜索“Bat ban jaegigentleman”,那么每个令牌都会以不同的方式进行搜索,并且每个令牌都会以不同的方式计算模糊距离。例如“Bat ban jaegigenteman”和“ban”之间的模糊度(编辑距离)将在 21 左右。因此,令牌模糊度系数都不会是 1。
    • 我观察到输入也没有被分析。 “raju”导致搜索成功,而“Raju”没有模糊度 = 0。另一个有趣的事实是,根据我的观察,如果模糊度大于 2,则模糊度默认为 2。 elastic.co/guide/en/elasticsearch/reference/7.6/… 下的模糊部分也提到了相同的内容
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多