gpt4 book ai didi

php - Elasticsearch 中可选的自父关系

转载 作者:可可西里 更新时间:2023-10-31 23:38:10 25 4
gpt4 key购买 nike

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

我的问题是,我需要为具有可选自关系的实体编制索引。这意味着我的 Item 实体有一个“父”字段,引用另一个 Item。

为了进行搜索,我想在该“父”字段上创建过滤器。 我的 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]] 

你知道为什么这不起作用吗?这是做到这一点的好方法吗?

谢谢,

最佳答案

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

配置

我没有在配置中使用 _parent 字段,而是使用了一个嵌套属性。

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

这是一棵树,所以子属性和父属性都是Item。我需要在父值上创建带有过滤器的请求(为空,等于某物...)

请求

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

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

$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))
);

我认为这个解决方案有局限性(我假设是针对多级育儿),但对于这种需要,它是有效的。

关于php - Elasticsearch 中可选的自父关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com