【问题标题】:Elastic search how to make query_string match Exact phrase弹性搜索如何使 query_string 匹配精确的短语
【发布时间】:2019-04-12 07:32:20
【问题描述】:

我需要 query_string 仅在完全相同时才匹配。

根据弹性documentation 关于查询字符串查询:

空格不被视为运算符,这意味着纽约市将“按原样”传递给为该字段配置的分析器。如果该字段是关键字字段,则分析器将创建单个词条 new york city,并且查询构建器将在查询中使用该词条。如果您想分别查询每个术语,则需要在术语周围添加显式运算符(例如 new AND york AND city)。

我创建了一个索引testingindex并添加了随机数据:

  • 版纳AF
  • cd 测试 af
  • 测试光盘
  • AF 电视
  • 测试ab

帖子:

POST testingindex/_doc/5
{
  "name":"banna af" 
}

搜索:

GET testingindex/_search?explain
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "fuzziness": 0, 
            "phrase_slop": 0, 
            "default_operator": "OR", 
            "minimum_should_match": "99%", 
            "fields": [
              "name"
            ],
            "query":"(testing af) OR (banna af)"
          }
        }
      ]
    }
  }
}

结果:

"hits" : [
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 2.0794415,
        "_source" : {
          "name" : "banna af"
        }
      },
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.8630463,
        "_source" : {
          "name" : "cd testing af"
        }
      },
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "testing cd"
        }
      },
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "af television"
        }
      },
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "testing ab"
        }
      }
    ]

如果我将运算符更改为:

"default_operator": "AND",

我得到了正确的结果。

但如果我将查询更改为:

    "query":"(testing af) OR (banna af) OR (badfadfaf)"

我没有得到结果,我仍然需要返回匹配的结果。

我怎样才能让 cd testing afbanna af 成为唯一返回的结果?

【问题讨论】:

标签: elasticsearch


【解决方案1】:

只需将术语本身包含在双引号中(您必须转义)以获得完全匹配并删除 minimum_should_match 属性 - 简化的查询如下所示:

GET testingindex/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "fields": [
              "name"
            ],
            "query":"(\"testing af\") OR (\"banna af\") OR (\"badfadfaf\")"
          }
        }
      ]
    }
  }
}

产量:

"hits" : {
    "total" : 2,
    "max_score" : 1.3862944,
    "hits" : [
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "qmD-EWoBqkB-aMRpwfuE",
        "_score" : 1.3862944,
        "_source" : {
          "name" : "banna af"
        }
      },
      {
        "_index" : "testingindex",
        "_type" : "_doc",
        "_id" : "q2D_EWoBqkB-aMRpFPtX",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "cd testing af"
        }
      }
    ]
  }

【讨论】:

  • 太棒了!谢谢!关于这个主题,我找到了几个非常复杂的答案,我喜欢你简单而务实的方法!
猜你喜欢
  • 1970-01-01
  • 2012-02-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多