如何對篩選以后聚合分組后的數(shù)據(jù)再進行范圍查詢呢?
老師好,項目中需要用到es來實現(xiàn)數(shù)據(jù)統(tǒng)計,現(xiàn)在有這樣一個需求不知道該如何實現(xiàn),查了一下資料,也沒有看到好的解決辦法
現(xiàn)在有這樣一組數(shù)據(jù)
[
{userId:1, name: 'liu', createTime:'2017-9-16', investMount: 1000, sex: 1},
{userId:1, name: 'liu', createTime:'2017-9-16', investMount: 2000, sex: 1},
{userId:1, name: 'liu', createTime:'2017-9-16', investMount: 3000, sex: 1},
{userId:2, name: 'wang', createTime:'2017-10-1', investMount: 1500, sex: 0},
{userId:3, name: 'zhang', createTime:'2017-10-14', investMount: 1800, sex: 1},
{userId:4, name: 'zhao', createTime:'2017-10-17', investMount: 4000, sex: 1}
]
比如:現(xiàn)在想查詢注冊時間(createTime)是2017-9-15到2017-10-15日之間,投資總額(同一個userId用戶investMount的總和)在2000-5000之間的男性(sex==1)的用戶,最后取到符合條件的userId的集合,這種需求該如何寫DSL語句呢?
我現(xiàn)在對es的學(xué)習(xí)能力只能想到這一步,先根據(jù)固定的條件進行查詢篩選,然后再根據(jù)用戶id進行分組,查詢每個用戶的投資總金額,但是如何使最后結(jié)果能返回符合所有條件的用戶userId的集合我還是想不出來,求指教~~
{
"query": {
"bool": {
"must": {
{"term": {"sex": 1}}
},
"filter": {
"range": {
"createTime": {
"from": "2017-9-15",
"to": "2017-10-15"
}
}
}
}
},
"aggs": {
"group_by_userId": {
"terms": {"field": "userId"},
"aggs": {
"sum_investMount": {
"sum": { "field": "investMount"}
}
}
}
}
}
2017-12-22
{
"query": {
"bool": {
"must": {
{"term": {"sex": 1}}
},
"filter": {
"range": {
"userId": {
"from": "1",
"to": "2
}
}
}
}
},
"aggs": {
"group_by_userId": {
"terms": {"field": "userId"},
"aggs": {
"sum_investMount": {
"sum": { "field": "investMount"}
}
}
}
}
}