我有一个Symfony2项目,我试图使用Elasticsearch实现搜索功能。Elasticsearch中的可选自亲关系
我的问题是,我需要索引一个可选的自我关系的实体。这意味着我的项目实体有一个“父”字段,引用另一个项目。
为了进行搜索,我想在该“父”字段上创建过滤器。 我的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]]
你有什么想法,为什么这不起作用?这是做这件事的好方法吗?
谢谢,
不应该** $ document-> setParent($ item-> getParent() - > getId()); **接收一个对象** $ document-> setParent($ item-> getParent()) ; **? – costa
感谢您的回答。 实际上,问题发生在值为null时(我尝试在该方法中手动设置一个id,并且它工作正常)。 – TiPi
我认为 - > getId()不正确,更改为'setParent($ item-> getParent())' –