Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.

Nested Filter

More info about nested filter is in the official elasticsearch docs

Nested filter allows to filter nested objects / documents (see nested mapping). The filter is executed against the nested objects / documents as if they were indexed as separate documents (they are, internally) and resulting in the root parent doc (or parent nested mapping).

Simple example

{
    "filtered" : {
        "query" : { "match_all" : {} },
        "filter" : {
            "nested" : {
                "path" : "obj1",
                "filter" : {
                    "bool" : {
                        "must" : [
                            {
                                "term" : {"obj1.name" : "blue"}
                            },
                            {
                                "range" : {"obj1.count" : {"gt" : 5}}
                            }
                        ]
                    }
                },
                "_cache" : true
            }
        }
    }
}

And now the query via DSL:

$termFilter = new TermFilter('obj1.name', 'blue');
$rangeFilter = new RangeFilter('obj1.count', ['gt' => 5]);

$boolFilter = new BoolFilter();
$boolFilter->add($termFilter);
$boolFilter->add($rangeFilter);

$nestedFilter = new NestedFilter('obj1', $boolFilter, ['_cache' => true]);

$search = new Search();
$search->addFilter($nestedFilter);

$queryArray = $search->toArray();