elasticsearchelasticsearch-queryphrase

Elastic Search match phrase query -> output not predictable


Sample doc

{
  "id": 5,
  "title": "Quick Brown fox jumps over the lazy dog",
  "genre": [
    "fiction"
  ]
}

Mapping

{
  "movies" : {
    "mappings" : {
      "properties" : {
        "genre" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

Query1: Results in the document shared earlier

{
 "query": {
   "match_phrase": {
     "title": {
       "query": "fox quick over", "slop": 3
     }
   }
 } 
}

Query2: No Results

{
 "query": {
   "match_phrase": {
     "title": {
       "query": "over fox quick", "slop": 3
     }
   }
 } 
}

I was expecting a result in query2 rather than in query 1.


Solution

  • Slop

    Number of times you need to move a term in order to make the query and document match.

    Switching word order requires two edits/steps

    Below is movement of words

    Query 1:

                Pos 1         Pos 2         Pos 3     Pos 4     Pos 5   Pos 6  Pos 7   Pos 8
    --------------------------------------------------------------------------------------
    Doc:        quick         brown         fox       jumps     over    the   lazy    dog
    ---------------------------------------------------------------------------------------
    Query:                                  fox       quick     over
    Slop 1:                                 fox|quick           over                                       
    Slop 2:                   quick         fox                 over
    Slop 3:    quick                        fox                 over
    

    total steps 3

    Query 2:

                Pos 1         Pos 2         Pos 3     Pos 4   Pos 5   Pos 6  Pos 7   Pos 8
    --------------------------------------------------------------------------------------
    Doc:        quick         brown         fox       jumps    over    the   lazy    dog
    ---------------------------------------------------------------------------------------
    Query:                    over          fox       quick
    Slop 1:                   over          fox|quick            
    Slop 2:                   quick|over    fox           
    Slop 3:     quick         over          fox       
    Slop 4:     quick                       over|fox      
    Slop 5:     quick                       fox       over
    Slop 6:     quick                       fox               over
    

    Total steps 6