Thursday, 12 November 2015

Elasticsearch: Boosting Query

The boosting query can be used to effectively demote results that match a given query.

For example, I want to get all the documents, where description contains “religious Buddhist Christian”. I want to give positive boost to the documents that contains word “religious” and negative boost to the documents that contain word “Buddhist Christian”.
GET /books/philosophy/_search
{
  "query": {
    "boosting": {
      "positive" :{
        "term": {
          "description" : "religious"
        }
      },
      "negative": {
        "term":{
          "description" : "Buddhist Christian"
        }
      },
      "negative_boost": 0.2
    }
  }
}

Above query returns following result.

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.2116434,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.2116434,
            "_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. Martins Griffin",
               "author": {
                  "firstName": "Osho",
                  "lastName": "Chandra Mohan Jain"
               },
               "price": 1500,
               "tags": [
                  "Spiritual",
                  "Philosophy",
                  "secrets"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "4",
            "_score": 0.109375,
            "_source": {
               "pages": 152,
               "title": "Emotions: Freedom from Anger, Jealousy and Fear",
               "description": "I am 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