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

ui7jx7zq  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(390)

有没有办法获得索引中所有文档的最大值和最小值,而不仅仅是类别“game”中的最大值和最小值,而无需向elastic发出另一个请求?

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

这是我的一些数据,通过上面的查询,我想从分类游戏和最小0,最大100,而不是最小10,最大20这2个文件。

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

fslejnso1#

是的,把枪拿开就行了 match 子句,并添加 match_all 查询以包含索引中的所有文档。使用post\u 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
    }
  }
}

相关问题