【发布时间】:2014-08-14 12:29:10
【问题描述】:
我有一个带有一些标签(多对多映射)的食谱实体,我想按标签搜索食谱。
这是我的食谱实体:
/**
* @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');
但尽管有匹配的结果,但我没有得到任何结果。
我在谷歌上没有找到任何答案。 任何人都可以帮助我吗?
【问题讨论】:
-
你解决了吗?答案好吗?
标签: symfony elasticsearch mapping nested-queries