我想查询到我的Elastic Search,以获得只有正值的值的最低价格。我的价格也可以是零和-1;所以我不希望我的最小聚合返回0或-1。我知道我应该向查询(或过滤器(添加一个脚本,但我不知道如何添加。我当前的代码:
public function minPriceAggregation($minName, $field = 'price')
{
if ($field) {
$this->aggregation[$minName] = [
'min' => [
'field' => $field
]
];
}
return $this;
}
此查询总是返回-1;我想忽略值<0,并且只有我的正值中的最小值。
类似于:
public function minPriceAggregation($minName, $field = 'price')
{
if ($field) {
$this->aggregation[$minName] = [
'min' => [
'field' => $field,
'script' => [
'source' => 'return _value > 0 ?: _value' //What's the correct script here??
]
]
];
}
return $this;
}
试试这个:
public function minPriceAggregation($minName, $field = 'price')
{
if ($field) {
$this->aggregation[$minName] = [
'min' => [
'script' => [
'source' => 'return Math.max(0, doc["price"].value)'
]
]
];
}
return $this;
}