我正在使用FOSElasticaBundle来集成我的symfony3项目和elasticsearch。我有一个类似如下的映射:
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 }我想按城市、州和嵌套businessAddress属性的邮政编码进行过滤。
我有这样的疑问:
$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":{"bool":{"must":[{"match":{"businessAddress.city":{"query":["NY"],"analyzer":"custom_analyzer"}}}]}}}但是有0个结果,我不知道sintax businessAddress.city是否会被捆绑包自动处理,或者我是否需要创建一个嵌套查询。如果这是一个嵌套查询,我该如何构建它?
编辑
在下面的一些注释之后,我注意到我将match term设置为数组,现在我从:
$cityQuery->setFieldQuery('businessAddress.city', array($city]));至
$cityQuery->setFieldQuery('businessAddress.city', $city]);json查询的结果:
{"query":{"bool":{"must":[{"match":{"businessAddress.state":{"query":"NY","analyzer":"custom_analyzer"}}}]}}}我上网查了一下,什么也没找到。
请帮帮忙。谢谢
发布于 2016-11-11 22:48:15
下面是如何使用嵌套查询实现它。
首先,您需要更新映射,使您的businessAddress成为nested类型
...
businessAddress:
type: nested
...然后,您将能够执行以下Nested查询
$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);希望这能有所帮助
https://stackoverflow.com/questions/40033778
复制相似问题