es - elasticsearch - aggs - bucket - terms

es - elasticsearch - aggs - bucket - terms,第1张

es - elasticsearch - aggs - bucket - terms

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问:terms有什么特点?
答:

问:terms如何使用?
答:

# 删除
DELETE /terms_agg_test

# 映射
PUT /terms_agg_test
{
  "mappings": {
    "properties": {
      "name": {"type": "keyword"},
      "filter_type": {"type": "keyword"},
      "num": {"type": "integer"}
    }
  }
}

# 索引
POST /terms_agg_test/_bulk?routing=1&refresh
{"index": {"_id": 1}}
{"name": "hello", "filter_type": "t1", "num": 3}
{"index": {"_id": 2}}
{"name": "me", "filter_type": "t2", "num": 5}
{"index": {"_id": 3}}
{"name": "me", "filter_type": "t1", "num": 18}
{"index": {"_id": 4}}
{"name": "hello", "filter_type": "t1", "num": 20}
{"index": {"_id": 5}}
{"name": "good", "filter_type": "t1", "num": 28}
{"index": {"_id": 6}}
{"name": "me", "filter_type": "t2", "num": 50}
{"index": {"_id": 7}}
{"name": "good", "filter_type": "t1", "num": 58}
{"index": {"_id": 8}}
{"name": "hello", "filter_type": "t1", "num": 88}

# 搜索
GET /terms_agg_test/_search?size=0
{
  "aggs": {
    "terms_aggs_type": {
      "terms": {
        "field": "filter_type",
        "size": 10,
        "shard_size": 10,
        "include": "t1",
        "show_term_doc_count_error": true
      },
      "aggs": {
        "terms_aggs_name": {
          "terms": {
            "field": "name",
            "size": 10,
            "exclude": ["me"],
            "min_doc_count": 3
          }
        }
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "terms_aggs_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "t1",
          "doc_count" : 6,
          "doc_count_error_upper_bound" : 0,
          "terms_aggs_name" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "hello",
                "doc_count" : 3
              }
            ]
          }
        }
      ]
    }
  }
}

# 搜索 - 使用子聚合排序
GET /terms_agg_test/_search?size=0
{
  "aggs": {
    "terms_aggs_type": {
      "terms": {
        "field": "filter_type",
        "size": 10,
        "shard_size": 10,
        "order": {
          "terms_aggs_num.avg": "desc"
        }
      },
      "aggs": {
        "terms_aggs_num": {
          "stats": {
            "field": "num"
          }
        }
      }
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "terms_aggs_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "t1",
          "doc_count" : 6,
          "terms_aggs_num" : {
            "count" : 6,
            "min" : 3.0,
            "max" : 88.0,
            "avg" : 35.833333333333336,
            "sum" : 215.0
          }
        },
        {
          "key" : "t2",
          "doc_count" : 2,
          "terms_aggs_num" : {
            "count" : 2,
            "min" : 5.0,
            "max" : 50.0,
            "avg" : 27.5,
            "sum" : 55.0
          }
        }
      ]
    }
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5070055.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存