我有一個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())' –