2016-02-09 17 views
4

我有一个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]] 

你有什么想法,为什么这不起作用?这是做这件事的好方法吗?

谢谢,

+0

不应该** $ document-> setParent($ item-> getParent() - > getId()); **接收一个对象** $ document-> setParent($ item-> getParent()) ; **? – costa

+0

感谢您的回答。 实际上,问题发生在值为null时(我尝试在该方法中手动设置一个id,并且它工作正常)。 – TiPi

+0

我认为 - > getId()不正确,更改为'setParent($ item-> getParent())' –

回答

1

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

配置

而是在配置使用_parent场的,我用了一个嵌套属性。

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

这是一棵树,所以孩子和父母的属性都是项目。 我需要与父值过滤器创建requess(null,则等于什么......)

请求

所以,我用嵌套在\ Elasitca \过滤器\。

举例来说,当我需要排除根据儿童的用户和语言的一些resultats,我可以做到以下几点:

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

我觉得这个解决方案具有局限性(多层次养育我假设),但为了这个需要,它可以工作。