Thursday, 12 November 2015

Elasticsearch: Dis max query


Let’s say I am maintaining an index ‘book’, and type  ‘philosophy’ to store all philosophy books.
PUT /_bulk
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : 1}}
{"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."}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : 2}}
{"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."}


I want to search for a book on “Osho Autobiography”, my query looks like below.
GET /books/philosophy/_search
{
  "query":{
    "bool" :{
      "should": [
        {"match" : {"title" : "Osho Autobiography"}},
        {"match" : {"description" : "Osho Autobiography"}}
      ]
    }
  }
}


You will get following kind of response.
{
   "took": 1,
   "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."
            }
         }
      ]
   }
}


As you observe the results “Osho philosophy” book comes first than the book “Autobiography of Osho”. This is because “bool” query works like below.

{
 "query" : {
  "bool" : {
   "should" : [
    {"match" : {"title" : "Osho Autobiography"}},
    {"match" : {"description" : "Osho Autobiography"}}
   ]
  }
 }
}

 
1.   “bool” query runs both of the queries in should clause.
2.   Adds scores together.
3.   Multiplies total by number of matching clauses.
4.   Divides the result by total number of clauses (2 clauses here).

As you observe document 2 contains the word “Osho” in both fields “title”, “description”.  But document1 don’t contain the word “Osho” in its description. So document 2 comes before document 1. But document 1 is the book on “osho autobiography”. It should come before document 2.

In the cases like above “dis_max” query is useful. It returns documents that match any of these queries, and return the score of the best matching query

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


You will get following response.

{
   "took": 4,
   "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."
            }
         }
      ]
   }
}





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment