Эластичный поиск & gt; Groovy скрипт & gt; Вложенные поля с множественным результатом в symfony

Как добавить условие и получить значение Величина платежа вложенное поле в totalRate?
Я использовал пакет Fos -astica в Symfony

Упругое поисковое отображение

profile:
mappings:
id: ~
title: ~
name: ~
rates:
type: "nested"properties:
id: ~
serviceName: ~
FeeAmount:
type: "float"

Я хочу условие, как я написал ниже в коде для Groovy сценария.
Здесь _source.rates () возвращает несколько результатов, поэтому мне нужно добавить отдельное условие для каждой услуги тарифов

    $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;

-1

Решение

Я нашел решение от 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;";
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]