弹性搜索 > Groovy 脚本 > 在 symfony 中具有多个结果的嵌套字段
Elastic search > Groovy script > Nested Fields with multiple result in symfony
如何在 totalRate 中添加条件并获取 FeeAmount 嵌套字段的值?
我在 symfony
中使用了 Fos-elastica 包
弹性搜索映射
profile:
mappings:
id: ~
title: ~
name: ~
rates:
type: "nested"
properties:
id: ~
serviceName: ~
FeeAmount:
type: "float"
我想要按照我在下面的代码中为 Groovy 脚本编写的条件。
这里,_source.rates() returns 多个结果,所以我需要为每个费率服务添加单独的条件
$elasticaClient = new \Elastica\Client();
$search = new \Elastica\Search($elasticaClient);
$search->addIndex('COC');
$search->addType('Profile');
$mainQuery = new \Elastica\Query(); // main query
// Start Groovy Script
$scriptString = "total = 0;
for (pos in _source.rates()) {
if(pos.id == 1){
total = total + pos.FeeAmount;
}
}
return total;";
$script = new \Elastica\Script($scriptString);
$script->setLang('groovy');
$mainQuery->setFields(['_source']);
$mainQuery->setScriptFields([ 'totalRate' => $script]);
$search->setQuery($mainQuery);
$results = $search->search()->getResults();
return $results;
我通过 google..
找到了解决方案
$scriptString = "total = 0;
for (pos in _source.rates()) {
if(pos.id == 1){
total = total + pos.FeeAmount;
}
}
return total;";
用这段代码替换上面的代码,就可以了
$scriptString = "
def total = 0;
for (rate in _source.rates) {
total = total + rate.FeeAmount;
}
return total;";
如何在 totalRate 中添加条件并获取 FeeAmount 嵌套字段的值? 我在 symfony
中使用了 Fos-elastica 包弹性搜索映射
profile:
mappings:
id: ~
title: ~
name: ~
rates:
type: "nested"
properties:
id: ~
serviceName: ~
FeeAmount:
type: "float"
我想要按照我在下面的代码中为 Groovy 脚本编写的条件。 这里,_source.rates() returns 多个结果,所以我需要为每个费率服务添加单独的条件
$elasticaClient = new \Elastica\Client();
$search = new \Elastica\Search($elasticaClient);
$search->addIndex('COC');
$search->addType('Profile');
$mainQuery = new \Elastica\Query(); // main query
// Start Groovy Script
$scriptString = "total = 0;
for (pos in _source.rates()) {
if(pos.id == 1){
total = total + pos.FeeAmount;
}
}
return total;";
$script = new \Elastica\Script($scriptString);
$script->setLang('groovy');
$mainQuery->setFields(['_source']);
$mainQuery->setScriptFields([ 'totalRate' => $script]);
$search->setQuery($mainQuery);
$results = $search->search()->getResults();
return $results;
我通过 google..
找到了解决方案 $scriptString = "total = 0;
for (pos in _source.rates()) {
if(pos.id == 1){
total = total + pos.FeeAmount;
}
}
return total;";
用这段代码替换上面的代码,就可以了
$scriptString = "
def total = 0;
for (rate in _source.rates) {
total = total + rate.FeeAmount;
}
return total;";