首页 > 解决方案 > 如何在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 的界限呢?

标签: elasticsearch

解决方案


推荐阅读