Wednesday 11 November 2015

Elasticsearch: Querying to match all the words

In previous post, I explained how to query for multi words. As you observe the output of previous post, match query performs “or” operation by default.
GET books/philosophy/_search
{
  "query":{
    "match": {
      "description": "young transformation Egyptian"
    }
  }
Above query returns all documents that contains words “young” or “transformation” or “Egyptian” in their description.


Suppose, if you want only documents that match all the terms, then use operator “and”.
GET books/philosophy/_search
{
  "query":{
    "match":{
        "description":{
          "query" : "young transformation Egyptian",
          "operator" : "and"
        } 
    }
  }
}
Above query returns no results, since there is no single document that contains all the three terms “young”, “transformation”, “Egyptian”.
GET books/philosophy/_search
{
  "query":{
    "match":{
        "description":{
          "query" : "young transformation",
          "operator" : "and"
        } 
    }
  }
}

Above query returns following response    
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.04746387,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "5",
            "_score": 0.04746387,
            "_source": {
               "pages": 240,
               "title": "Veronika Decides to Die: A Novel of Redemption",
               "description": "tells the story of a young woman's transformation from despairing would-be suicide to affirmed and then affirming survivor. This book offers an archetypal story of hope, portraying a situation in which joy, freedom, integrity and truth all remain possible under the most challenging and limiting of circumstances. In doing so, the narrative thematically explores the nature of insanity, the importance of living a genuine life, and the threats to individual identity imposed by closed communities and the rules under which they function.",
               "ISBN": "5955000836",
               "publisher": "Harper Perennial",
               "author": {
                  "firstName": "Mitch",
                  "lastName": "Albom"
               },
               "price": 420,
               "tags": [
                  "literature",
                  "fiction"
               ]
            }
         }
      ]
   }
}






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment