Saturday, 7 November 2015

Elasticsearch: Full Text Search

Full text search is used to find related documents. As compared to structural search, it is not for finding exact terms, it is to identify related documents.
For example, you can find related news articles for given news item.
To explain examples related to full text search, I am going to use below sample data.

POST /_bulk
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "1"}}
{"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\u0027s Griffin","author":{"firstName":"Osho","lastName":"Chandra Mohan Jain"},"price":1500.0,"tags":["Spiritual","Philosophy","secrets"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "2"}}
{"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\u0027s Griffin","author":{"firstName":"Osho","lastName":"Chandra Mohan Jain"},"price":350.0,"tags":["Spiritual","Philosophy","courage","Living joyfully"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "3"}}
{"pages":152,"title":"Emotions: Freedom from Anger, Jealousy and Fear","description":"I\u0027m 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.0,"tags":["Spiritual","Philosophy","emotions","freedom","angry","Jealousy","Fear"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "4"}}
{"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.0,"tags":["literature","fiction","religion","spirituality"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "5"}}
{"pages":240,"title":"Veronika Decides to Die: A Novel of Redemption","description":"tells the story of a young woman\u0027s 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.0,"tags":["literature","fiction"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "6"}}
{"pages":220,"title":"The Republic","description":"Plato\u0027s most famous work and one of the most important books ever written on the subject of philosophy and political theory","ISBN":"1420931695","publisher":"Digireads.com","author":{"firstName":"Plato","lastName":"Plato"},"price":342.0,"tags":["life","philosophy","conversations"]}
{"create" : {"_index" : "books", "_type":"philosophy", "_id" : "7"}}
{"pages":224,"title":"Tuesdays with Morrie: An Old Man, a Young Man, and Life\u0027s 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.0,"tags":["life","philosophy","mankind","conversations"]}
Relevance
When you query an index/type, the documents are returned in descending order of relevance. “_score” field of query results identifies relevance. Higher the “_score” field, more the document is related.
Find all documents that contains word religious.

GET books/philosophy/_search
{
  "query":{
    "match": {
      "description": "religious"
    }
  }
}


Above query returns following documents.

{
   "took": 10,
   "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"
               ]
            }
         }
      ]
   }
}
As you observe the response, two documents are returned in descending order of _score field (relevance).



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment