Querying Multi Level Nested fields on Elastic Search Querying Multi Level Nested fields on Elastic Search elasticsearch elasticsearch

Querying Multi Level Nested fields on Elastic Search


Ok, after trying a tone of combinations, I finally got it using the following query:

query3 = {    "nested": {        "path": "subjects",        "score_mode": "avg",        "query": {            "bool": {                "must": [                    {                        "text": {"subjects.concepts.name": "concept1"}                    }                ]            }        }    }}

So, the nested path attribute (subjects) is always the same, no matter the nested attribute level, and in the query definition I used the attribute's full path (subject.concepts.name).


Shot in the dark since I haven't tried this personally, but have you tried the fully qualified path to Concepts?

query2 = {       "nested": {           "path": "subjects.concepts",           "score_mode": "avg",           "query": {               "bool": {                   "must": [                       {                           "text": {"subjects.concepts.name": "concept1"}                       },                       {                          "range": {"subjects.concepts.score": {"gt": 0}}                       }                   ]               }           }       }    } 


I have some question for JCJS's answer. why your mapping shouldn't like this?

mapping = {    "subjects": {        "type": "nested",        "properties": {            "concepts": {                "type": "nested"            }        }    }}

I try to define two type-mapping maybe doesn't work, but be a flatten data; I think we should nested in nested properties..

At last... if we use this mapping nested query should like this...

{    "query": {        "nested": {            "path": "subjects.concepts",            "query": {                "term": {                    "name": {                        "value": "concept1"                    }                }            }        }    }}

It's vital for using full path for path attribute...but not for term key can be full-path or relative-path.