使用slop进行ElasticSearch bool查询,如何实现?

时间:2018-05-16 19:09:52

标签: elasticsearch

我有一个查询,我需要找到N个单词,文档也必须匹配正则表达式字符串。我有这个工作,但我也要求术语必须在3个单词之间,在弹性语法slop:3。问题是在bool查询中不允许slop。

{
  "from": 0,
  "size": 100,
  "explain": true,
  "_source": {
    "includes": [
      "*"
    ],
    "excludes": [
      "doctext"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "doctext": {
              "query": "value1 value2"
            }
          }
        },
        {
          "regexp": {
            "doctext": {
              "value": "[0-9]{3}"
            }
          }
        }
      ]
    }
  }
}

有没有人知道实现相同结果的另一种方式?

1 个答案:

答案 0 :(得分:0)

match查询替换为match_phrase并向其添加slop,例如

{
  "from": 0,
  "size": 100,
  "explain": true,
  "_source": {
    "excludes": [
      "doctext"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "doctext": {
              "query": "value1 value2",
              "slop": 3
            }
          }
        },
        {
          "regexp": {
            "doctext": {
              "value": "[0-9]{3}"
            }
          }
        }
      ]
    }
  }
}