【问题标题】:Query string in elastic search弹性搜索中的查询字符串
【发布时间】:2021-07-10 09:50:38
【问题描述】:

我正在使用以下匹配查询搜索弹性搜索,这并没有给我完全匹配,而是给我一些更不重要的匹配。

我正在使用弹性搜索 6.3

请在下面找到我的查询

GET /_search
{
   "must":{
      "query_string":{
         "query":"review:*test product*"
      }
   }
}

搜索结果:

“命中”:[{“_index”:“67107104”,“_type”:“_doc”,“_id”:“1”,“_score”:0.6931471,“_source”:{“title”:“测试" } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "product good" } } ,{“_index”:“67107104”,“_type”:“_doc”,“_id”:“3”,“_score”:0.6931471,“_source”:{“title”:“sample”}},{“_index ": "67107104", "_type": "_doc", "_id": "4", "_score": 0.7897571, "_source": { "title": "superr" } } ]

预期的搜索结果:

“命中”:[{“_index”:“67107104”,“_type”:“_doc”,“_id”:“1”,“_score”:0.6931471,“_source”:{“title”:“测试" } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "product good" } } ]

【问题讨论】:

    标签: elasticsearch match query-string match-phrase


    【解决方案1】:

    如果您没有明确定义任何映射,则需要将 .keyword 添加到 title 字段。这使用关键字分析器而不是标准分析器(注意标题字段后的“.keyword”)。

    添加一个包含索引数据、搜索查询和搜索结果的工作示例

    索引数据:

    {
      "title": "This is test product"
    }
    {
      "title": "test product"
    }
    

    搜索查询:

    {
      "query": {
        "query_string": {
          "fields": [
            "title.keyword"
          ],
          "query": "test product"
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "67107104",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.6931471,
            "_source": {
              "title": "test product"
            }
          }
        ]
    

    使用匹配查询的搜索查询:

    {
      "query": {
        "match": {
          "title.keyword": "test product"
        }
      }
    }
    

    使用术语查询进行搜索查询

        {
          "query": {
            "term": {
              "title.keyword": "test product"
            }
          }
        }
    

    【讨论】:

    • @revathi 您是否有机会查看答案,期待您的反馈:-)
    • 我想同时执行完全单词匹配和部分单词/子字符串匹配。例如,如果我搜索“测试产品”,那么我应该能够在结果中找到“测试”和“产品”相关的文本。给出不重要的匹配例如它在结果中给出“样本”相关的标题。
    • @revathi 您是否尝试过我的答案中显示的搜索查询?您能否分享您的示例索引数据,以便我提供一个可行的示例?
    • 结果:“hits”:[“_index”:“67107104”,“_type”:“_doc”,“_id”:“1”,“_score”:0.6931471,“_source”:{ “标题”:“测试”}},{“_index”:“67107104”,“_type”:“_doc”,“_id”:“2”,“_score”:0.6931471,“_source”:{“title”: “产品好”}},{“_index”:“67107104”,“_type”:“_doc”,“_id”:“3”,“_score”:0.6931471,“_source”:{“title”:“sample” }}] 预期结果:“hits”:[ {“_index”:“67107104”,“_type”:“_doc”,“_id”:“1”,“_score”:0.6931471,“_source”:{“title” :“测试”}},{“_index”:“67107104”,“_type”:“_doc”,“_id”:“2”,“_score”:0.6931471,“_source”:{“title”:“产品好" }}]
    • @revathi 在您的示例索引数据中没有包含{ "title": "sample" } 的文档,那么您如何在搜索结果中获取此数据?
    【解决方案2】:

    您可以使用布尔查询与过滤器通过使用术语进行完全匹配。由于该术语用于精确匹配,因此您需要为文本字段添加关键字

    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "review_title.keyword": "test product"
              }
            }
          ]
        }
      }
    }

    【讨论】:

    • 但我也需要子字符串匹配。这就是我使用 query_string 的原因。请找到我预期的搜索结果
    • 尝试使用通配符查询进行部分匹配或获得更好的用户 ngram 体验
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多