Elasticsearch: Searching on nested data

Elasticsearch provides nested relationship, here is how we can use it.
Nested data is the data that us related to the main document and can be searched through






elasticsearch nested search

I have been working with Elasticsearch(mentioned as ES) for almost 2 years now. Have used it on 3 projects, but I would say i am still learning it. There have been times when I reviewed my old ES queries and found them incorrect. Here is portion of my experience i would like to share.It is easy to search ES with root level data but what abut nested data.

Elasticsearch provides nested relationship, here is how we can use it. Nested data is the data that us related to the main document and can be searched through. Example: product has categories. the chances are a single product can belong to different categories. Each category can have its own details like slug, image, description.

Similarly main document in ES can have list of hospitals and each hospital offers various treatments. So there are chances where you need to search products by category and hospitals by treatment.

For such kind of relation within document we need to specify mapping to ES for a particular document type. Example mapping for hospital

                    {
            "properties":{
               "hospital_address1":{
                  "type":"string"
               },
               "hospital_address2":{
                  "type":"string"
               },
               "hospital_description":{
                  "type":"string"
               },
               "hospital_id":{
                  "type":"integer"
               },
               "hospital_name":{
                  "type":"string"
               },
               "hospital_slug":{
                  "type":"string",
                  "index":"not_analyzed"
               },
               "hospital_status":{
                 "type":"string",
                 "index":"not_analyzed"
               },
               "treatments":{
                 "type":"nested", -- WE SAY THAT TREATMENTS ARE NESTED IN HOSPITALS
                  "properties":{
                     "child_of":{
                        "type":"integer"
                     },
                     "hospital_id":{
                        "type":"integer"
                     },
                     "id":{
                        "type":"integer"
                     },
                     "keywords":{
                        "type":"string"
                     },
                     "status":{
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "treatment_id":{
                        "type":"integer"
                     },
                     "treatment_name":{
                        "type":"string"
                     },
                     "treatment_price":{
                        "type":"long"
                     },
                     "treatment_slug":{
                        "type":"string",
                        "index":"not_analyzed"
                     },
                     "treatment_status":{
                        "type":"string",
                        "index":"not_analyzed"
                     }
                  }
               }
            }
         }

Now the search request would be as follows

// THIS IS A SAMPLE CODE TO SHOW HOW THE QUERY WOULD LOOK LIKE
// this code is of a sample boolean query
{
"sort": [
                    {
                        "hospital_name": {
                            "order": "asc"
                        }
                    }
                ],
                "query": {
                    "bool": {
                        "must": [
                            {
                                "nested": { // -- have look here
                                    "path": "treatments", //  -- and here
                                    "query": {
                                        "bool": {
                                            "must": {
                                                "term": {
                                                    "treatments.treatment_slug": "cardiology"
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            {
                                "term": {
                                    "hospital_status": "active"
                                }
                            }
                        ],
                        "minimum_should_match": 2
                    }
                }
            }

read more here

Thank you.