elasticsearch Elastic:query_string两个句子之间的近似搜索

ykejflvf  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(147)

你好,
我需要将此旧查询(Exalead)转换为Elasticsearch:

"One Sentence" NEAR Word

我试探着:

"One Sentence" AND "Word"~16

但它将此查询解释为:

"One Sentence" AND "Word"

而“一句话”与“单词”的距离大于16个单词。
那么我如何使用query_string_query执行此查询?

whlutmcx

whlutmcx1#

您可以使用intervals query
根据匹配术语的顺序和接近程度返回文档。
间隔查询使用从一小组定义中构造的匹配规则。然后将这些规则应用于指定字段中的字词。

{
  "query": {
    "intervals": {
      "text": {
        "all_of": {
          "ordered": false,
          "max_gaps": 16,  --> distance between both phrases
          "intervals": [
            {
              "match": {
                "query": "one sentence",
                "max_gaps": 0,--> distance between each phrase
                "ordered": false
              }
            },
            {
              "match": {
                "query": "word",
                "max_gaps": 0,
                "ordered": false
              }
            }
          ]
        }
      }
    }
  }
}

使用查询字符串

(可选,整数)短语的匹配标记之间允许的最大位置数。默认值为0。如果为0,则要求短语完全匹配。转置的术语的斜率为2。

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "text",
            "query": "\"One sentence\""
          }
        },
        {
          "query_string": {
            "default_field": "text",
            "query": "\"sentence word\"",
            "phrase_slop": 4
          }
        }
      ]
    }
  }
}

相关问题