弹性搜索中的查询字符串

时间:2021-04-15 10:50:27

标签: elasticsearch match query-string match-phrase

我正在使用下面的匹配查询搜索弹性搜索,它没有给我精确的匹配,而是给出了一些更不重要的匹配。

我正在使用弹性搜索 6.3

请在下面找到我的查询

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

搜索结果:

"hits": [ { "_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "title": "testing " } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "产品好" } } , { "_index": "67107104", "_type": "_doc", "_id": "3", "_score": 0.6931471, "_source": { "title": "sample" } },{ "_index" ": "67107104", "_type": "_doc", "_id": "4", "_score": 0.7897571, "_source": { "title": "superr" } } ]

预期搜索结果:

"hits": [ { "_index": "67107104", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "title": "testing " } }, { "_index": "67107104", "_type": "_doc", "_id": "2", "_score": 0.6931471, "_source": { "title": "产品好" } } ]

2 个答案:

答案 0 :(得分: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"
        }
      }
    }

答案 1 :(得分:0)

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

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

相关问题