Saturday 14 November 2015

Elasticsearch: match_phrase query slop parameter


In previous post, I explained how match_phrase query works. “match_phrase” query match documents, if it founds the query string in same order. But there are situations; you want to match documents even if there is some distance between tokens.

For Example
You want to match the document, for the string “professor travelled india” in description field of documents.

As you observe document1 contains data like “A professor of philosophy, he travelled throughout India during the 1960s as a public speaker ……………..”

The terms of query phrase “professor travelled india appeared in same order in document1, but in between some more terms exist. In these cases, simple “match_phrase” query returns no documents. You can use “slop” parameter to solve these kinds of scenarios.
GET /books/philosophy/_search
{
  "query": {
    "match_phrase": {
      "description" :{
        "query": "professor travelled india",
        "slop" : 4
      }
    }
  }
}
You will get following response.
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.06432597,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.06432597,
            "_source": {
               "title": "Autobiography of Osho",
               "description": "A professor of philosophy, he travelled throughout India during the 1960s as a public speaker. His outspoken criticism of politicians and the political mind, Mahatma Gandhi and institutionalised religion made him controversial."
            }
         }
      ]
   }
}





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment