如何使用Elastica Search和Symfony2执行嵌套查询 [英] How to perform nested queries with Elastica Search and Symfony2

查看:132
本文介绍了如何使用Elastica Search和Symfony2执行嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个食谱实体,它有一些标签(多对多的映射),我想通过标签搜索食谱。

I have a recipe entity which have some tags (many to many mapping) and I want to search recipes by tags.

这是我的食谱实体:

/**
 * @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: ~

然后我添加了一个自定义存储库来执行搜索: p>

I then added a custom repository to perform searches:

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

但是,尽管有匹配的结果,但是我没有任何结果。

But I got no results whatsoever despite the fact there are matching results.

我没有在google上找到任何答案。
任何人都可以帮助我吗?

I didn't find any answers on google. Anyone can help me ?

推荐答案

的确,你忘了定义Filtered查询。

Indeed, you forgot to define the Filtered query.

这应该工作:

$nested->setQuery(new \Elastica\Query\Term(array('name' => array('value' => $searchText))));
$nested->setPath('tags');
$query_part->addShould($nested);

$query = new \Elastica\Query\Filtered($query_part/*, $filters // in case you had other filters*/);
return $this->find($query);

这篇关于如何使用Elastica Search和Symfony2执行嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆