elastic6.5聚合分析
DELETE /myindex POST /myindex/_doc/_bulk {"index":{"_id":"1"}} {"songName":"take me to your heart", "singer":"Michael Learns To Rock", "lyrics":"Hiding from the rain and snow,Trying to forget but I won’t let go,Looking at a crowd"} {"index":{"_id":"2"}} {"songName":"take me to your heart", "singer":"Michael Learns To Rock", "lyrics":"Hiding from the rain and snow,Trying to forget but I won’t let go,Looking at a crowd"} {"index":{"_id":"3"}} {"songName":"take me to your heart", "singer":"Emily", "lyrics":"Hiding from the rain and snow,Trying to forget but I won’t let go,Looking at a crowd"} {"index":{"_id":"4"}} {"songName":"take me to your heart", "singer":"Kody", "lyrics":"Hiding from the rain and snow,Trying to forget but I won’t let go,Looking at a crowd"} {"index":{"_id":"5"}} {"songName":"take me to your heart", "singer":"Tony", "lyrics":"Hiding from the rain and snow,Trying to forget but I won’t let go,Looking at a crowd"} {"index":{"_id":"6"}} {"songName":"you are beautiful", "singer":"James Blunt", "lyrics":" angel.Of that I‘m sure. ,She smiled at me on"} {"index":{"_id":"7"}} {"songName":"you are beautiful", "singer":"James Blunt", "lyrics":" angel.Of that I‘m sure. ,She smiled at me on"} {"index":{"_id":"8"}} {"songName":"you are beautiful", "singer":"James Blunt", "lyrics":" angel.Of that I‘m sure. ,She smiled at me on"} {"index":{"_id":"9"}} {"songName":"you are beautiful", "singer":"James Blunt", "lyrics":" angel.Of that I‘m sure. ,She smiled at me on"} {"index":{"_id":"10"}} {"songName":"you are beautiful", "singer":"James Blunt", "lyrics":" angel.Of that I‘m sure. ,She smiled at me on"} GET /myindex/_doc/4
GET /bank/_search?size=0 { "aggs": { "by_state": { "terms": { "field": "state.keyword", "size": 10 }, "aggs": { "avg_age": { "avg": { "field": "age" } } } } } } GET /bank/_search/ #每种状态,平均年龄 # select state,avg(age) # from bank group by state GET /bank/_search?size=0 { "aggs": { "state_agg": { "terms": { "field": "state.keyword" }, "aggs": { "avg_age": { "avg": { "field": "age" } } } } } } #每种状态,平均年龄 # select state,avg(age) avg_age # from bank group by state # having avg_age <=30; #平均年龄小于30岁的状态 GET /bank/_search?size=0 { "aggs": { "state_agg": { "terms": { "field": "state.keyword" }, "aggs": { "avg_age": { "avg": { "field": "age" } }, "es_having" : { "bucket_selector": { "buckets_path": { "kody_avg" : "avg_age" }, "script": "params.kody_avg <=30" } } } } } } GET /bank/_search #年龄为25岁的用户,男生、女分别有多少人 # select gender,count(*) # from bank where age=25 # group by gender; POST /bank/_search?size=0 { "query": { "term": { "age": { "value": 25 } } }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } # filter agg POST /bank/_search?size=0 { "aggs": { "25yearold": { "filter": { "term": { "age": 25 } }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } } } #年龄为20、21、25岁的用户,男生、女生分别有多少人 #filters #where age in (21, 20, 25) #group by age,gender GET /bank/_search?size=0 { "query": { "terms": { "age": [ 21, 20, 25 ] } }, "aggs": { "by_age": { "terms": { "field": "age" }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } } } #年龄为20、21、25岁的用户,男生、女生分别有多少人 #filters GET /bank/_search?size=0 { "aggs": { "20_21_25": { "filters": { "filters": { "20": {"term": {"age": 20}}, "21": {"term": {"age": 21}}, "25": {"term": {"age": 25}} } }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } } } #每个年龄段,男生\女生 分别多少人 #10~20 20~30 30~40 40~50 GET /bank/_search?size=0 { "aggs": { "age_age": { "filters": { "filters": { "10~20": { "range": { "age": { "gte": 10, "lt": 20 } } }, "20~30" : { "range": { "age": { "gte": 20, "lt": 30 } } }, "30~40" : { "range": { "age": { "gte": 30, "lt": 40 } } }, "40~50" : { "range": { "age": { "gte": 40, "lt": 50 } } } } }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } } } #range GET /bank/_search?size=0 { "aggs": { "age_range": { "range": { "field": "age", "ranges": [ { "from": 10, "to": 20 }, { "from": 20, "to": 30 }, { "from": 30, "to": 40 }, { "from": 40, "to": 50 } ] }, "aggs": { "by_gender": { "terms": { "field": "gender.keyword" } } } } } } GET /bank/_search #最近一个月,注册了多少用户 # date_range GET /bank/_search?size=0 { "aggs": { "month_recen": { "date_range": { "field": "registered", "ranges": [ { "from": "now-1M/M", "to": "now" } ] } } } } #每个月 用户注册人数 #date_histogram POST /bank/_search?size=0 { "aggs": { "by_month": { "date_histogram": { "field": "registered", "interval": "month" } } } }