Optional Self-Parent Relation in Elasticsearch Optional Self-Parent Relation in Elasticsearch elasticsearch elasticsearch

Optional Self-Parent Relation in Elasticsearch


Since my needs are pretty simple in this case, we managed to solve this problem.

Configuration

Instead of using the _parent field in config, I used a nested property.

Item:    mappings:        children:            type: "nested"            properties:                name: ~        parent:            type: "object"

This is a tree, so children and parent properties are both Item. I needed to create requess with filter on the parent value (is null, is equals something...)

Request

So, I used the \Elasitca\Filter\Nested.

For instance, when I need to exclude some resultats based on children's user and language, I can do the following :

$nestedFilter  = new Nested();$boolFilter    = new Bool();$boolAndFilter = new BoolAnd();$boolAndFilter    ->setFilters(array(        new Term(array("children.user.id" => $this->getClient()->getId())),        new Term(array("children.other" => $language))    ));$nestedFilter    ->setFilter($boolAndFilter)    ->setPath('children');$query = new Filtered(    $query,    new BoolNot($boolFilter->addMust($nestedFilter)));

I think this solution has limitation (for multi-level parenting I assume), but for this need, it works.