'And' effect search for multiple nested object

I have a complicated mapping where multiple levels of nested objects contain information e.g. level3_attr1 and level4_attr1 I want to search.

mappings : {
    mytype: {
        properties: {
            level1tag: {
                properties: {
                    level2tag: {
                        type: "nested", 
                        properties: {
                            ...
                            level3tag: {
                                type: "nested",
                                properties: {
                                    ...
                                    level3_attr1: {
                                        type: "text", 
                                        fields: { keyword: { type: "keyword", ignore_above: 256 }  }
                                    } 
                                    level4tag: {
                                        type: "nested",
                                        properties: {
                                            ...
                                            level4_attr1: {
                                                type: "text", 
                                                fields: { keyword: { type: "keyword", ignore_above: 256 }  }
                                            } 
                                        }
                                    }// end level4tag
                                    ...
                                }
                            }// level3tag
                        } 
                    }// level2tag    
                }
            }// level1tag 
        }
    }
}

Now I want to achieve the effect like SQL's ... where level3_attr1 = 'value A' and level4_attr1 = 'value B' ... through search API. But specifying query as below

{ 
    "query": { 
        "bool" : { 
            "must": [ 
                { 
                    "nested": { 
                        "path": "level1tag.leve2tag.level3tag.level4tag", 
                        "query": { 
                            "bool": { 
                                "must": [ 
                                    { 
                                        "term": { 
                                            "level1tag.level2tag.level3tag.level4tag.level4_attr1.keyword": "value A" 
                                        }
                                    } 
                                ] 
                            } 
                        } 
                    } 
                }, 
                { 
                    "nested": { 
                        "path": "level1.level2.level3", 
                        "query": { 
                            "bool": { 
                                "must": [ 
                                    { 
                                        "term" : {
                                            "level1tag.level2tag.level3tag.level3_attr1.keyword": "value B" 
                                          } 
                                    } 
                                ] 
                            } 
                        } 
                    }  
                } 
            ] 
        } 
    }
}

returns the result with effect like or (i.e. level3_attr1 = 'value A' or level4_attr1 = 'value B') where multiple docs having one of the attributes match returned.

So my question is how can I achieve the effect like and when using search API in a multiple nested object mapping index?

Come across the the link - Querying a multiple level nested object. That basically helps solve my problem.

Instead of separated paths on different nested object query,

{
    "query": {
        "bool": {
            "must": [
                { ... nested query on path1 }
                { ... nested query on path2 }
            ]
        }
    }
}

query should be specified with base path then down to the deeper level. So each level can specify its own condition.

"query": {
    "nested": {
        "path": "level1tag.level2tag.level3tag",
        "query": {
            "bool": {
                "must": [
                    {
                        "term": { "level1tag.level2tag.level3tag.level3_attr1": "value A" }
                    },
                    {
                        "nested": {
                            "path": "level1tag.level2tag.level3tag.level4tag",
                            "query": {
                                "bool": {
                                     "must" : [
                                         { "term": { "level1tag.level2tag.level3tag.level4tag.level4_attr1" : "value B"}  }
                                      ]
                                }
                            } 
                        }    
                    }
                ]
            }
        }
    }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.