我有一个食谱实体,其中有一些标签(多对多Map),我想通过标签搜索食谱。
以下是我的Recipe实体:
/**
* @ORM\Entity
* @ORM\Table(name="recipes")
* @ORM\HasLifecycleCallbacks
* @ExclusionPolicy("all")
*/
class Recipe
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose
*/
protected $id;
/**
* @ORM\Column(type="string", length=150)
* @Expose
*/
protected $name;
...
/**
* @ORM\ManyToMany(targetEntity="RecipesTag", inversedBy="recipes")
* @ORM\JoinTable(name="bind_recipes_tags",
* joinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="recipe_id", referencedColumnName="id")}
* )
*/
private $tags;
以下是我的配置:
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
serializer:
callback_class: FOS\ElasticaBundle\Serializer\Callback
serializer: serializer
indexes:
td:
client: default
types:
Recipe:
mappings:
name: ~
ingredients:
type: "nested"
properties:
name: ~
tags:
type: "nested"
properties:
name: ~
id :
type : integer
categories:
type: "nested"
properties:
name: ~
id :
type : integer
persistence:
driver: orm # orm, mongodb, propel are available
model: ck\RecipesBundle\Entity\Recipe
repository: ck\RecipesBundle\Entity\RecipesRepository
provider: ~
listener: ~
finder: ~
然后,我添加了一个自定义存储库来执行搜索:
public function filterFind($searchText)
{
$query_part = new \Elastica\Query\Bool();
$nested = new \Elastica\Query\Nested();
$nested->setQuery(new \Elastica\Query\Term(array('name' => array('value' => $searchText))));
$nested->setPath('tags');
$query_part->addShould($nested);
return $this->find($query_part);
}
然后我这样搜索:
$repositoryManager = $this->get('fos_elastica.manager.orm');
$repository = $repositoryManager->getRepository('ckRecipesBundle:Recipe');
$recipes = $repository->filterFind('mytag');
但我没有得到任何结果,尽管事实上有匹配的结果。
我在谷歌上找不到任何答案。有人能帮助我吗?
2条答案
按热度按时间dtcbnfnu1#
下面是如何做到这一点回应对我来说,这是可行http://obtao.com/blog/2014/04/elasticsearch-advanced-search-and-nested-objects/
基本上,您需要创建如下查询:
在PHP中,它看起来像:
过滤器为Elastica\过滤器,查询为Elastica\查询
jv2fixgn2#
实际上,您忘记了定义Filtered查询。
这应该可行: