Saturday 14 November 2015

Elastisearch: match_phrase query

“match_phrase” query gets the documents that contain the query string in same order.

GET /books/philosophy/_search


Above query return following response.

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 1,
            "_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."
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "2",
            "_score": 1,
            "_source": {
               "title": "Osho philosophy",
               "description": "Osho Autobiography is a book on philosophy. Osho travelled throughout India during the 1960s as a public speaker. Osho outspoken criticism of politicians and the political mind, Mahatma Gandhi and institutionalised religion made him controversial.Osho written many books on philosophy."
            }
         }
      ]
   }
}


GET /books/philosophy/_search
{
  "query": {
    "match_phrase": {
      "title": "Autobiography of Osho"
    }
  }
}


Above query returns following response.

{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.46027923,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.46027923,
            "_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."
            }
         }
      ]
   }
}


If you run same query using “match” instead of “match_phrase” you will get both documents in response.

Note:
"match_phrase" query is a special form of match query. You can write the same using “match” query also.
GET /books/philosophy/_search
{
  "query": {
    "match": {
      "title": {
        "query" : "Autobiography of Osho",
        "type" : "phrase"
      }
    }
  }
}







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment