表结构:
GET ehirestate/_mapping
{ "ehirestate" : { "mappings" : { "ehire" : { "properties" : { "activeTime" : { "type" : "long" }, "ctmId" : { "type" : "integer" }, "date" : { "type" : "integer" }, "downloadCount" : { "type" : "long" }, "inboxExport" : { "type" : "long" }, "inboxSearch" : { "type" : "long" }, "inboxView" : { "type" : "long" }, "loginCount" : { "type" : "long" }, "otherView" : { "type" : "long" }, "resumeSearch" : { "type" : "long" }, "resumeTemDown" : { "type" : "long" }, "resumeView" : { "type" : "long" }, "userId" : { "type" : "integer" } } } } } }
实例一:
功能:1- 对用户userId进行装桶,对resumeView进行聚合后排序;
2- 对date进行范围性匹配;
3- 对会员ctmid进行精确匹配;
4- 返回结果是聚合后的loginCount和resumeView
查询语句:
GET ehirestate/_search?size=0 { "query": { "bool" : { "filter" : [ { "term" : { "ctmId" : { "value" : 1475127 } } }, { "range": { "date": { "gte": 20191201, "lte": 20191231 } } } ] } }, "aggs": { "father": { "terms": { "field": "userId",
"size": 1, "order": { "child": "desc" } }, "aggs": { "child": { "sum": { "field": "resumeView" } }, "loginCount":{ "sum": { "field": "loginCount" } } } } } }
返回结果:
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 339, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "father" : { "doc_count_error_upper_bound" : -1, "sum_other_doc_count" : 338, "buckets" : [ { "key" : 6231765, "doc_count" : 1, "loginCount" : { "value" : 3.0 }, "child" : { "value" : 476.0 } } ] } } }
功能二:
对聚合后的排序后取出对应的文档数据
查询语句:
GET ehirestate/_search { "query": { "bool": { "filter": [ { "term": { "ctmId": { "value": 1475127 } } }, { "range": { "date": { "gte": 20191201, "lte": 20191231 } } } ] } }, "size": 0, "aggs": { "father": { "terms": { "field": "userId", "order": { "top_hit": "desc" }, "size": 1 }, "aggs": { "top_tags_hits": { "top_hits": {} }, "top_hit": { "sum": { "script": { "source": "doc.resumeView" } } } } } } }
返回结果:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 339, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "father" : { "doc_count_error_upper_bound" : -1, "sum_other_doc_count" : 338, "buckets" : [ { "key" : 6231765, "doc_count" : 1, "top_hit" : { "value" : 476.0 }, "top_tags_hits" : { "hits" : { "total" : 1, "max_score" : 1.4E-45, "hits" : [ { "_index" : "ehirestate", "_type" : "ehire", "_id" : """1475127623176520191203""", "_score" : 0.0, "_routing" : "1475127", "_source" : { "date" : 20191203, "activeTime" : 0, "inboxView" : 0, "resumeTemDown" : 0, "userId" : 6231765, "loginCount" : 3, "inboxExport" : 0, "otherView" : 0, "inboxSearch" : 1, "resumeSearch" : 73, "ctmId" : 1475127, "downloadCount" : 2, "resumeView" : 476 } } ] } } } ] } } }