简体   繁体   中英

FOSElasticaBundle multiple nested query

I use FOSElasticaBundle to handle searching. All works great when I have one level of nesting. However, when I have two levels of nesting results which should match the innermost nest are not returned (eg searching for 'xx' category does produce results, but searching for 'yy' brand does not - and should).

Here's my fos_elastica configuration:

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: ~

And my query handler:

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

How is the $nestedBrandCategoryQuery added to the $mainProductQuery ?

Thanks for your help! gtb

FOSElasticaBundle uses the Elastica Library. So this should not be an issue of FOSElasticaBundle.

Have a Look at http://elastica.io/ for more Details about the Lib. In my experience,there is nothing you can not do with Elastica if it is supported by Elasticsearch. Even when there is no Mapper in Elastica, just use the Raw Array Query ( http://elastica.io/example/raw-array-query.html ) to build the desired Query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM