Wednesday, 11 November 2015

Elasticsearch: match query

Match query is used for full text search.

For example, following match query return all documents, that contains word religious in description field.
GET books/philosophy/_search
{
  "query":{
    "match": {
      "description": "RELIGIOUS"
    }
  }
}

Above query returns following response.
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.125,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.125,
            "_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": "3",
            "_score": 0.033562027,
            "_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"
               ]
            }
         }
      ]
   }
}
What happen when you query description field?
1. As you see description field is of type string, so query string should be analyzed before querying.
2. Query string “RELIGIOUS” passed through standard analyzer, which converts word “RELIGIOUS” to “religious”.
3. Elastic search looks for the term “religious” in description field of all the documents.
4. “_score” is calculated for all the matched documents and results are returned in descending order of “_score” value.

2. I want to search for documents where tags contains words “spiritual” “freedom” or “angry”.

GET books/philosophy/_search
{
  "query":{
    "match": {
      "tags": "spiritual freedom angry"
    }
  }
}


Above query returns following response.

{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.19930676,
      "hits": [
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "3",
            "_score": 0.19930676,
            "_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"
               ]
            }
         },
         {
            "_index": "books",
            "_type": "philosophy",
            "_id": "1",
            "_score": 0.06422861,
            "_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.056200027,
            "_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