查询中所有索引的最大值和最小值

Max and min from all index in query

有没有办法在不向 elastic 发出另一个请求的情况下获取索引中所有文档的最大值和最小值,而不仅仅是来自类别“游戏”的最大值和最小值?

{
"query": {
    "bool": {
        "must": [
            {
                "match": {
                    "category": "game"
                }
            }
        ]
    }
},
"aggs": {
    "maxPoints": {
        "max": {
            "field": "points"
        }
    },
    "minPoints": {
        "min": {
            "field": "points"
        }
    }
}

这是我拥有的一些数据数据,通过上面的查询我想从游戏类别中获取这 2 个文档,最小 0,最大 100 而不是最小 10,最大 20。

[
  {
    "id": 1,
    "category": "offer",
    "points": 0
  },
  {
    "id": 2,
    "category": "game",
    "points": 10
  },
  {
    "id": 3,
    "category": "game",
    "points": 20
  },
  {
    "id": 4,
    "category": "offer",
    "points": 100
  }
]

是的,只需删除 match 子句,并添加 match_all 查询以将所有文档包含在您的索引中。使用 post_filter 在单个 ES 调用中获得预期结果。

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "maxPoints": {
      "max": {
        "field": "points"
      }
    },
    "minPoints": {
      "min": {
        "field": "points"
      }
    }
  },
  "post_filter": { // Note this 
    "term": {
      "category": "game"
    }
  }
}

输出

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "65406564",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "id": 2,
          "category": "game",
          "points": 10
        }
      },
      {
        "_index": "65406564",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "id": 3,
          "category": "game",
          "points": 20
        }
      }
    ]
  },
  "aggregations": {
    "maxPoints": {
      "value": 100.0
    },
    "minPoints": {
      "value": 0.0
    }
  }
}