FOSElasticaBundle多个嵌套查询

时间:2014-01-28 09:46:33

标签: symfony elasticsearch

我使用FOSElasticaBundle来处理搜索。当我有一个嵌套级别时,一切都很棒。然而,当我有两个级别的嵌套结果应该匹配最内层的嵌套时不会返回(例如,搜索'xx'类别确实会产生结果,但搜索'yy'品牌不会 - 并且应该)。

这是我的fos_elastica配置:

fos_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    my_index:
        client: default
        types:
            product:
                mappings:
                    title: { boost: 1 }
                    articleNumber: ~
                    introductionDateSearch: { type: integer }
                    delistingDateSearch: { type: integer }
                    deleted: { type: boolean }
                    category:
                        type: "nested"
                        properties:
                            name: { boost: 1 }
                            brand:
                                type: "nested"
                                properties:
                                    name: { boost: 1 }
                persistence:
                    driver: orm
                    model: MyBundle\Entity\Product
                    provider: ~
                    finder: ~
                    listener: ~

我的查询处理程序:

public function searchForKeyword($keyword, AbstractUser $user)
{
    $this->setFilters($user);
    $keyword = trim($keyword);

    if ($keyword !== '') {
        $mainQuery = new \Elastica\Query\Bool();
        $mainProductQuery = new \Elastica\Query\Bool();

        //searching in Product title
        $productQuery = new \Elastica\Query\Text();
        $productQuery->setFieldQuery('title', $keyword);
        $productQuery->setFieldParam('title', 'boost', 5);
        $productQuery->setFieldParam('title', 'type', 'phrase_prefix');

        //searching in Product articleNumber
        $articleNumberQuery = new \Elastica\Query\Text();
        $articleNumberQuery->setFieldQuery('articleNumber', $keyword);
        $articleNumberQuery->setFieldParam('articleNumber', 'boost', 5);
        $articleNumberQuery->setFieldParam('articleNumber', 'type', 'phrase_prefix');

        //searching in Category name
        $categoryQuery = new \Elastica\Query\Text();
        $categoryQuery->setFieldQuery('name', $keyword);
        $categoryQuery->setFieldParam('name', 'boost', 3);
        $categoryQuery->setFieldParam('name', 'type', 'phrase_prefix');

        $nestedCategoryProductQuery = new \Elastica\Query\Nested();
        $nestedCategoryProductQuery->setPath('category');
        $nestedCategoryProductQuery->setQuery($categoryQuery);

        //searching in Brand name
        $brandQuery = new \Elastica\Query\Text();
        $brandQuery->setFieldQuery('name', $keyword);
        $brandQuery->setFieldParam('name', 'boost', 3);
        $brandQuery->setFieldParam('name', 'type', 'phrase_prefix');

        $nestedBrandCategoryQuery = new \Elastica\Query\Nested();
        $nestedBrandCategoryQuery->setPath('category.brand');
        $nestedBrandCategoryQuery->setQuery($brandQuery);

        $mainProductQuery->addShould($productQuery);
        $mainProductQuery->addShould($articleNumberQuery);
        $mainProductQuery->addShould($nestedCategoryProductQuery);
        $mainProductQuery->addShould($nestedBrandCategoryQuery);

        $mainQuery->addMust($mainProductQuery);

        $esFilteredQuery = new \Elastica\Query\Filtered($mainQuery, $this->filters);

    } else {
        $esFilteredQuery = new \Elastica\Query\Filtered(new \Elastica\Query\MatchAll(), $this->filters);
    }

    $this->query = new \Elastica\Query();
    $this->query->setQuery($esFilteredQuery);
}

如何将$nestedBrandCategoryQuery添加到$mainProductQuery

感谢您的帮助! GTB

1 个答案:

答案 0 :(得分:0)

FOSElasticaBundle使用Elastica库。所以这不应该是FOSElasticaBundle的问题。

查看http://elastica.io/以获取有关Lib的更多详细信息。根据我的经验,如果Elasticsearch支持Elastica,则无法做任何事情。即使Elastica中没有Mapper,只需使用原始数组查询(http://elastica.io/example/raw-array-query.html)来构建所需的查询。