世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问:composite有什么特点?
答:
问:composite如何使用?
答:
# 删除 DELETE /composite_test # 映射 PUT /composite_test { "mappings": { "properties": { "name": {"type": "keyword"}, "num": {"type": "integer"}, "date": {"type": "date"} } } } # 索引 POST /composite_test/_bulk?routing=1&refresh {"index": {"_id": 1}} {"name": "hello", "num": 3, "date": "2021-09-29"} {"index": {"_id": 2}} {"name": "good", "num": 5, "date": "2020-09-29"} {"index": {"_id": 3}} {"name": "me", "num": 5, "date": "2024-09-29"} {"index": {"_id": 4}} {"name": "hello", "num": 7, "date": "2024-09-29"} # 搜索 GET /composite_test/_search { "size": 0, "aggs": { "com_aggs": { "composite": { "sources": [ { "names": { "terms": { "field": "name" } } } ] } } } } # 结果 { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "com_aggs" : { "after_key" : { "names" : "me" }, "buckets" : [ { "key" : { "names" : "good" }, "doc_count" : 1 }, { "key" : { "names" : "hello" }, "doc_count" : 2 }, { "key" : { "names" : "me" }, "doc_count" : 1 } ] } } } # 搜索 GET /composite_test/_search { "size": 0, "aggs": { "com_aggs": { "composite": { "sources": [ { "nums": { "histogram": { "field": "num", "interval": 2 } } } ] } } } } # 结果 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "com_aggs" : { "after_key" : { "nums" : 6.0 }, "buckets" : [ { "key" : { "nums" : 2.0 }, "doc_count" : 1 }, { "key" : { "nums" : 4.0 }, "doc_count" : 2 }, { "key" : { "nums" : 6.0 }, "doc_count" : 1 } ] } } } # 搜索 GET /composite_test/_search { "size": 0, "aggs": { "com_aggs": { "composite": { "sources": [ { "dates": { "date_histogram": { "field": "date", "calendar_interval": "1d", "format": "yyyy-MM-dd" } } } ] } } } } # 结果 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "com_aggs" : { "after_key" : { "dates" : "2024-09-29" }, "buckets" : [ { "key" : { "dates" : "2020-09-29" }, "doc_count" : 1 }, { "key" : { "dates" : "2021-09-29" }, "doc_count" : 1 }, { "key" : { "dates" : "2024-09-29" }, "doc_count" : 2 } ] } } } # 搜索 - 组合统计 GET /composite_test/_search { "size": 0, "aggs": { "com_aggs": { "composite": { "sources": [ { "names": { "terms": { "field": "name" } } },{ "nums": { "histogram": { "field": "num", "interval": 2 } } },{ "dates": { "date_histogram": { "field": "date", "calendar_interval": "1d", "format": "yyyy-MM-dd" } } } ] } } } } # 结果 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "com_aggs" : { "after_key" : { "names" : "me", "nums" : 4.0, "dates" : "2024-09-29" }, "buckets" : [ { "key" : { "names" : "good", "nums" : 4.0, "dates" : "2020-09-29" }, "doc_count" : 1 }, { "key" : { "names" : "hello", "nums" : 2.0, "dates" : "2021-09-29" }, "doc_count" : 1 }, { "key" : { "names" : "hello", "nums" : 6.0, "dates" : "2024-09-29" }, "doc_count" : 1 }, { "key" : { "names" : "me", "nums" : 4.0, "dates" : "2024-09-29" }, "doc_count" : 1 } ] } } }