简体   繁体   中英

How do I add a condition to ElasticSearch Aggregation (min/max)?

I want to query to my Elastic-Search to get minimum price of values which have only positive value. My prices can also be zero and -1; so I don't want my min-aggregation to return 0 or -1. I know that I should add a script to the query (or filter), but I don't know how. My current code:

public function minPriceAggregation($minName, $field = 'price')
    {
        if ($field) {
            $this->aggregation[$minName] = [
                'min' => [
                    'field' => $field
                ]
            ];
        }
        return $this;
    }

This query always returns -1; I want to ignore the values < 0 and only have the minimum in my positive values.

Something like:

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

Try this:

public function minPriceAggregation($minName, $field = 'price')
    {
        if ($field) {
            $this->aggregation[$minName] = [
                'min' => [
                    'script' => [
                        'source' => 'return Math.max(0, doc["price"].value)'
                    ]
                ]
            ];
        }
        return $this;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM