如何在Elasticsearch中设置聚合条件

时间:2019-10-15 08:51:10

标签: elasticsearch

假设我有一个索引

  "products": {
    "aliases": {},
    "mappings": {
      "products": {
        "properties": {
          "id": {
            "type": "long"
          },
          "price": {
            "type": "double"
          },
          "discount": {
            "type": "double"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

我需要获得所有折扣在0-10之间,价格在5-20之间且总价格应低于200的产品。

我知道如何过滤字段

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "price": {
              "from": 5,
              "to": 200,
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        },
        {
          "range": {
            "discount": {
              "from": 0,
              "to": 10,
              "include_lower": true,
              "include_upper": true,
              "boost": 1.0
            }
          }
        }
      ]
    }
  }
}

此外,我知道如何汇总价格

  "aggregations": {
    "total_price": {
      "sum": {
        "field": "price"
      }
    }
  }

但是如何设置此total_price的界限?

0 个答案:

没有答案