Elasticsearch中的可选自我父母关系

时间:2016-02-09 11:35:17

标签: php symfony elasticsearch foselasticabundle

我有一个Symfony2项目,我正在尝试使用Elasticsearch实现搜索功能。

我的问题是,我需要使用可选的自我关系索引实体。这意味着我的商品实体有一个"父母"字段,引用另一个项目。

为了进行搜索,我想在那个" parent"上创建过滤器。领域。 我的Item.parent是否为NULL?例如。

所以,我正在使用FosElasticaBundle。

这是我的映射:

    types:
    Item:
        mappings:
            name:
            children:
                type: object
                _parent:
                    type: Item
            parent:
                type: object
        _routing:
            required: false
        _parent:
            type : Item
            identifier: id
            property : parent
        persistence:
            ...
            model_to_elastica_transformer:
                service: core.transformer.item

变压器确实:

$document = new Document();

if (!is_null($item->getParent())) {
    $document->setParent($item->getParent()->getId());
} else {
     $document->setParent(null);
}

return $document;

而且,当我尝试创建索引(php app/console fos:elastica:populate

时会出现问题

此命令返回以下ResponseException:

index: /traveler/Item caused RoutingMissingException[routing is required for [traveler]/[Item]/[null]] 

你知道为什么这不起作用吗?这是一个很好的方法吗?

谢谢,

1 个答案:

答案 0 :(得分:1)

由于我的需求在这种情况下非常简单,我们设法解决了这个问题。

<强>配置

我没有在config中使用func('something', 'other', port='8000') 字段,而是使用了嵌套属性。

_parent

这是一棵树,因此子和父属性都是Item。 我需要使用父值的过滤器创建请求(为空,等于某事......)

请求

所以,我使用了\ Elasitca \ Filter \ Nested。

例如,当我需要根据孩子的用户和语言排除某些结果时,我可以执行以下操作:

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

我认为这个解决方案有局限性(对于我假设的多层次教养),但是对于这种需要,它可以工作。