Thursday 12 November 2015

Elasticsearch: Boosting Queries


Let’s take a scenario, suppose we want to get all the philosophy books in data store that are written by osho.
GET books/philosophy/_search
{
  "query":{
    "match" : {
      "author.firstName" : "osho" 
    }
  }
}

Response like below.
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 1,
            "_source": {
               "pages": 1152,
               "title": "The Book of Secrets",
               "description": "These techniques will not mention any religious ritual. No temple is needed, you are quite enough of a temple yourself. You are the lab; the whole experiment is to go on within you. This is not religion, this is science. No belief is needed. Only a daringness to experiment is enough; courage to experiment is enough.",
               "ISBN": "0312180586",
               "publisher": "St. Martin's Griffin",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 1500,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "secrets"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "2",
            "_score": 1,
            "_source": {
               "pages": 208,
               "title": "Courage: The Joy of Living Dangerously",
               "description": "In the beginning there is not much difference between the coward and the courageous person. The only difference is, the coward listens to his fears and follows them, and the courageous person puts them aside and goes ahead. The courageous person goes into the unknown in spite of all the fears.",
               "ISBN": "0312205171",
               "publisher": "St. Martin's Griffin",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 350,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "courage",
                  "Living joyfully"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "3",
            "_score": 0.30685282,
            "_source": {
               "pages": 152,
               "title": "Emotions: Freedom from Anger, Jealousy and Fear",
               "description": "I'm not a Buddhist, but a Christian. I started reading, just to see what it was about and read the whole thing. The beginning is a very relaxing read and had some highlightable/clipable quotes. I do have different religious beliefs, this book offered some areas for further prayer and contemplation but I did not find these areas offensive so much as a beneficial exposure to a different perspective",
               "ISBN": "1938755928",
               "publisher": " Osho Media International",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 400,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "emotions",
                  "freedom",
                  "angry",
                  "Jealousy",
                  "Fear"
               ]
            }
         }
      ]
   }
}


Now, I want to query for books written by osho, at the same time, I want to give more weightage to the tag “secrets”, less weightage to the tags “angry”, “emotions”. I can do this by using “boost” field that passed as part of my query. “boost” parameter is used to increase the relative weight of a clause in query. “boost” value greater than 1 increase the weight, less than 1 decrease the weight.

GET books/philosophy/_search
{
  "query":{
    "bool" :{
     "must": [
       {"match": {
         "author.firstName": "osho"
       }}
     ],
     "should": [
       {"match": {"tags" : {
         "query" : "angry emotons",
         "boost" : 3
       }}},
       {"match": {"tags" : {
         "query" : "secrets",
         "boost" : 5
       }}}
     ]
      
    }
  }
}


You will get following response.

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.26487556,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.26487556,
            "_source": {
               "pages": 1152,
               "title": "The Book of Secrets",
               "description": "These techniques will not mention any religious ritual. No temple is needed, you are quite enough of a temple yourself. You are the lab; the whole experiment is to go on within you. This is not religion, this is science. No belief is needed. Only a daringness to experiment is enough; courage to experiment is enough.",
               "ISBN": "0312180586",
               "publisher": "St. Martin's Griffin",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 1500,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "secrets"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "2",
            "_score": 0.029901665,
            "_source": {
               "pages": 208,
               "title": "Courage: The Joy of Living Dangerously",
               "description": "In the beginning there is not much difference between the coward and the courageous person. The only difference is, the coward listens to his fears and follows them, and the courageous person puts them aside and goes ahead. The courageous person goes into the unknown in spite of all the fears.",
               "ISBN": "0312205171",
               "publisher": "St. Martin's Griffin",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 350,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "courage",
                  "Living joyfully"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "3",
            "_score": 0.016592726,
            "_source": {
               "pages": 152,
               "title": "Emotions: Freedom from Anger, Jealousy and Fear",
               "description": "I'm not a Buddhist, but a Christian. I started reading, just to see what it was about and read the whole thing. The beginning is a very relaxing read and had some highlightable/clipable quotes. I do have different religious beliefs, this book offered some areas for further prayer and contemplation but I did not find these areas offensive so much as a beneficial exposure to a different perspective",
               "ISBN": "1938755928",
               "publisher": " Osho Media International",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 400,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "emotions",
                  "freedom",
                  "angry",
                  "Jealousy",
                  "Fear"
               ]
            }
         }
      ]
   }
}





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment