gpt4 book ai didi

elasticsearch - 将汇总限制为值列表

转载 作者:行者123 更新时间:2023-12-02 22:46:55 25 4
gpt4 key购买 nike

我可以限制聚合只返回特定的值列表吗?我有这样的事情:

{ "aggs" : {
"province" : {
"terms" : {
"field" : "province"
}
}
},
"query": {
"bool": {
//my query..

但是,假设我知道要计数的省份列表({'province1','province2','province3'})。是否可以限制返回的省份列表而不影响我的查询结果?

我想得到:
  //list of hits..
//
"aggregations": {
"province": {
"buckets": [
{
"key": "province1",
"doc_count": 200
},
{
"key": "province2",
"doc_count": 162
},
{
"key": "province3",
"doc_count": 162
}
// even if there is more possible provinces
// I don't want to see them

最佳答案

当然,只需使用term filters即可。

这是一个例子。假设我访问了许多不同IP地址的统计信息,但是我只想获取其中两个的文档计数,我可以这样做:

POST /test_index/_search?search_type=count
{
"aggregations": {
"ip": {
"terms": {
"field": "ip",
"size": 10,
"include": [
"146.233.189.126",
"193.33.153.89"
]
}
}
}
}

并返回类似:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"ip": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "146.233.189.126",
"doc_count": 3
},
{
"key": "193.33.153.89",
"doc_count": 3
}
]
}
}
}

这是我以前玩过的一些代码:

http://sense.qbox.io/gist/68697646ef7afc9f0375995b6f84181a7ac4cba9

因此,您的示例可能如下所示:
{
"aggs": {
"province": {
"terms": {
"field": "province",
"include": [
"province1",
"province2",
"province3"
]
}
}
}
}

关于elasticsearch - 将汇总限制为值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com