Phrase_slop does not affect query_string

My search query

{
    "query_string": {"query": 'foo bar',
                                 "phrase_slop": 1,
                                 "default_operator": "AND"}
}

returns the following results:

  1. 'foo bar'
  2. 'foo baz bar'
  3. 'foo bip bop bip bop bip bop bar'
  4. 'foo baz bip bop bip bop bip bop bar baz'

1 and 2 are matched because the slop is 1, so I'm allowing one token between 'foo' and 'bar'.
I'm expecting 3 and 4 not to match, because there are more than one token between 'foo' and 'bar'.

What I'm missing?
How to make it work as I intended?

Hi @MultiheadAttention

Documentation Query String query

Returns documents based on a provided query string, using a parser with a strict syntax.

This query uses a syntax to parse and split the provided query string based on operators, such as AND or NOT. The query then analyzes each split text independently before returning matching documents.

Try use match_phrase

{
  "query": {
    "match_phrase": {
      "text": {
        "query": "foo bar",
        "slop": 1
      }
    }
  }
}
1 Like

Thanks for your reply.

'query_string' documentation also says:

phrase_slop

(Optional, integer) Maximum number of positions allowed between matching tokens for phrases.

Defaults to 0. If 0, exact phrase matches are required. Transposed terms have a slop of 2.

And I'd like to make it work in 'query_string'.

I've tried to work with 'match_phrase' query and the 'slop' parameter works perfectly. The thing is that I also want to use logic operators, especially NOT in my query, and I'm not sure if it's possible in 'match_phrase' query.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.