Sergii Golubev
Sergii Golubev

Reputation: 398

elasticsearch: phrase search for two adjacent words in any order (analyzed)

The problem is to do a phrase search for two adjacent words in any order with words analysis.

E.g. in Sphinx extended syntax terms the query string can be written as WordToBeAnalyzed1 NEAR/1 WordToBeAnalyzed2. Then both words are being analyzed and the search engine finds either "Word1 Word2" or "Word2 Word1", where both words can be in any form (e.g. "fox jumps", "jumping fox", "foxes jumped", and so on).

Reading the ES docs I could not express the same search in the ES query DSL.

When querying with match_phrase and slop I can query a phrase "WordToBeAnalyzed1 WordToBeAnalyzed2" with a "slop": 2 param to match same words in reverse order. But it will also match such undesirable variants as "Word1 SlopWord1 Word2" and "Word1 SlopWord1 SlopWord2 Word2".

I also tried to use span_near query with the in_order param, but

span queries are term-level queries, so they have no analysis phase

I would be glad if anyone can point me to a way to solve this problem.

Upvotes: 3

Views: 1942

Answers (2)

Chittiraju Yenni
Chittiraju Yenni

Reputation: 11

This will work.

{
"query":{
    "bool":{
        "must":[
            {
                "query_string":{
                    "query":"*hello* *there*",
                    "fields":[
                        "subject"
                    ],
                    "default_operator":"and",
                }
            }]
      }
  }
}

Upvotes: 1

BenG
BenG

Reputation: 1312

What about running the query through an explicit request to the _analyze API first, then the span_near query?

Upvotes: 2

Related Questions