Wednesday, 11 November 2015

Elasticsearch: Querying for multi words

By using elastic search, you can query for multiple words at a time. For example, if you want to find all the documents, where it’s description contains any of the words “young”, “transformation”, “Egyptian”. Query looks like below.
GET books/philosophy/_search
{
  "query":{
    "match": {
      "description": "young transformation Egyptian"
    }
  }
}


Response like below.
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.016057152,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "7",
            "_score": 0.016057152,
            "_source": {
               "pages": 224,
               "title": "Tuesdays with Morrie: An Old Man, a Young Man, and Life's Greatest Lesson",
               "description": "Maybe it was a grandparent, or a teacher, or a colleague. Someone older, patient and wise, who understood you when you were young and searching, helped you see the world as a more profound place, gave you sound advice to help you make your way through it.",
               "ISBN": "076790592X",
               "publisher": "Broadway Books",
               "author": {},
               "price": 172,
               "tags": [
                  "life",
                  "philosophy",
                  "mankind",
                  "conversations"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "5",
            "_score": 0.012596527,
            "_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"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "4",
            "_score": 0.0023722053,
            "_source": {
               "pages": 197,
               "title": "The Alchemis",
               "description": "While sleeping near a sycamore tree in the sacristy of an abandoned church, Santiago, a shepherd boy, has a recurring dream about a child who tells him that he will find a hidden treasure if he travels to the Egyptian pyramids. An old woman tells Santiago that this dream is prophetic and that he must follow its instructions. Santiago is uncertain, however, since he enjoys the life of a shepherd.",
               "ISBN": "0061122416",
               "publisher": "HarperOne",
               "author": {
                  "firstName": "Mitch",
                  "lastName": "Albom"
               },
               "price": 380,
               "tags": [
                  "literature",
                  "fiction",
                  "religion",
                  "spirituality"
               ]
            }
         }
      ]
   }
}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment