gpt4 book ai didi

symfony - foselasticabundle嵌套查询

转载 作者:行者123 更新时间:2023-12-03 00:11:54 25 4
gpt4 key购买 nike

我使用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"],"anal‌​yzer":"custom_analyz‌​er"}}}]}}}

但是有0个结果,我不知道 bundle 包是否会自动处理sintax businessAddress.city还是我需要创建一个嵌套查询。如果这是一个嵌套查询,我如何构建它?

编辑

在下面的一些评论之后,我注意到我将匹配项设置为数组,现在我从:
$cityQuery->setFieldQuery('businessAddress.city', array($city]));


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

结果在json查询上:
{"query":{"bool":{"must":[{"match":{"businessAddress.state":{"query":"NY","analyzer":"custom_analyzer"}}}]}}}

我已经通过互联网检查,什么也没找到。

请帮忙。谢谢

最佳答案

这是使用嵌套查询实现它的方法。

首先,您需要更新映射以使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);

希望这可以帮助

关于symfony - foselasticabundle嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40033778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com