Thursday 12 November 2015

Elasticsearch: bool query


It is just like bool filter, instead of making yes/no decisions, it is used to get relevant results.

GET books/philosophy/_search
{
  "query":{
    "bool": {
      "must":{
        "match" : {"tags" : "philosophy"}
      },
      "must_not": [
        {"match" : {"description" : "grandparent teacher"}},
        {"match" : {"tags" : "conversations  angry"}}
      ]
    }
  }
}

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



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment