简体   繁体   中英

foselasticabundle nested queries

Im using FOSElasticaBundle to integrate my symfony3 project with elasticsearch. I have a mapping similar to:

company:
    mappings:
        id: { boost: 1, type: string, index: not_analyzed }
        cik: { boost: 2, type: string, index: not_analyzed }
        conformedName: { boost: 6, analyzer: custom_analyzer }
        assignedSIC:
            type: object
            properties:
                id: { type: string, index: not_analyzed }
                code: { type: string, index: not_analyzed }
                title: { analyzer: custom_analyzer }
        businessAddress:
            type: object
            properties:
                street1: { analyzer: custom_analyzer }
                street2: { analyzer: custom_analyzer }
                city: { analyzer: custom_analyzer }
                state: { analyzer: custom_analyzer }
                zip: { type: string, index: not_analyzed }
                phone: { type: string, index: not_analyzed }

I want to filter by city, state and zip of the nested businessAddress property.

I have this query:

$boolQuery = new BoolQuery();
$cityQuery = new Match();
$cityQuery->setFieldQuery('businessAddress.city', array($city]));
$cityQuery->setFieldParam('businessAddress.city', 'analyzer', 'custom_analyzer');
$boolQuery->addMust($cityQuery);
$this->finder->find($boolQuery);

json query as

{"query":{"bool":{"must":[{"match":{"businessAddress.city":{‌​"query":["NY"],"anal‌​yzer":"custom_analyz‌​er"}}}]}}}

But have 0 results, i dont know if the sintax businessAddress.city will be handled automatically by the bundle or do i need to create a nested query. In case that is a nested query how i can build that?

EDIT

After some comments below i notice i was setting match term as array, now i change from:

$cityQuery->setFieldQuery('businessAddress.city', array($city]));

to

$cityQuery->setFieldQuery('businessAddress.city', $city]);

resulting on json query:

{"query":{"bool":{"must":[{"match":{"businessAddress.state":{"query":"NY","analyzer":"custom_analyzer"}}}]}}}

i have check over internet and found nothing.

Please help. Thanks

Here is how to implement it with a nested query.

First of all you need to update your mapping to make your businessAddress a type nested

...
    businessAddress:
        type: nested
...

Then you will be able to perform the following Nested query

$boolQuery = new BoolQuery();
$cityQuery = new Match();
$cityQuery->setFieldQuery('businessAddress.city', array($city]));
$cityQuery->setFieldParam('businessAddress.city', 'analyzer', 'custom_analyzer');
$boolQuery->addMust($cityQuery);
// nested query
$query = new Nested();
$query->setPath('businessAddress');
$query->setQuery($boolQuery);
// pass the nested query to your finder
$this->finder->find($query);

Hope this helps

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