Elasticsearch complex proximity query
Asked Answered
B

1

7

Given that I have a query like below:

council* W/5 (tip OR tips)

The above query can be translated as: Find anything that has council* and (tip OR tips) no more than 5 words apart.

So following text will match:

  • Shellharbour City Council Tip
  • council best tip
  • councils top 10 tips

But this one should not match:

  • ... City Council at Shellharbour. There is not any good tip at all.

I need help to build an elasticsearch query for that. I was thinking about Regex query but I'm not quite sure about better alternatives. Thanks

Bovid answered 4/3, 2014 at 3:26 Comment(0)
A
9

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.

Affranchise answered 4/3, 2014 at 16:21 Comment(1)
Thank mate, that's exactly what I'm looking for. My queries are very complicated, I'll have to implement something to translate those queries using these span_* elasticsearch query.Bovid

© 2022 - 2024 — McMap. All rights reserved.