Saturday 14 November 2015

Elasticsearch: Multi match query

If you want to apply same search query on multiple fields, then you can use multi_match query. By default, this query runs as type best_fields, which means that it generates a match query for each field and wraps them in a dis_max query.

GET /books/philosophy/_search
{
  "query":{
    "dis_max" :{
      "queries": [
        {"match" : {"title" : "Osho Autobiography"}},
        {"match" : {"description" : "Osho Autobiography"}}
      ]
    }
  }
}


We can rewrite above query using multi_match like below.

GET /books/philosophy/_search
{
  "query":{
    "multi_match": {
      "query": "Osho autobiography",
      "fields": ["title", "description"]
    }
  }
}


Types of multi match query
You can specify how this multi_match query to execute. There are five different values associated with the field “type” for “multi_match” query.

1.best_fields
Finds documents, which match any field, but uses the _score from the best field. This is the default type for multi match query.

GET /books/philosophy/_search
{
  "query":{
    "multi_match": {
      "query": "Osho autobiography",
      "fields": ["title", "description"],
      "type": "best_fields"
    }
  }
}


Response like below.

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


2. most_fields
Finds documents, which match any field and combines the _score from each field.

GET /books/philosophy/_search
{
  "query":{
    "multi_match": {
      "query": "Osho autobiography",
      "fields": ["title", "description"],
      "type": "most_fields"
    }
  }
}


Response like below.

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


3. cross_fields
Treats fields with the same analyzer as though they were one big field. Looks for each word in any field.

4. phrase
Runs a match_phrase query on each field and combines the _score from each field.

5. phrase_prefix
Runs a match_phrase_prefix query on each field and combines the _score from each field.

References






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment