Elasticsearch complex proximity query

夙愿已清 提交于 2019-12-04 07:44:54

You can use a combination of the span_near query, span_multi and span_or. We can use the query below to perform the same search.

{
  "query": {
    "span_near": {
      "clauses": [
        {
          "span_multi":
          {
            "match":
            {
              "prefix": { "text": "council"}
            }
          }
        },
        {
          "span_or": {
            "clauses": [
              {
                "span_term": {
                  "text": {
                    "value": "tip"
                  }
                }
              },
              {
                "span_term": {
                  "text": {
                    "value": "tips"
                  }
                }
              }
            ]
          }
        }
      ],
      "slop": 5,
      "in_order": true
    }
  }
}

The important things to look out for are the span_term which is the text your searching for. In this example I only had one field called "text". Slop indicates the number of words we will allow between the terms, and in_order indicates that the order of words is important. So "tip council" will not match, where as "council tip" will.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!