慕雪2507746
2017-11-16 16:11
老师好,项目中需要用到es来实现数据统计,现在有这样一个需求不知道该如何实现,查了一下资料,也没有看到好的解决办法
现在有这样一组数据
[
{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}
]
比如:现在想查询注册时间(createTime)是2017-9-15到2017-10-15日之间,投资总额(同一个userId用户investMount的总和)在2000-5000之间的男性(sex==1)的用户,最后取到符合条件的userId的集合,这种需求该如何写DSL语句呢?
我现在对es的学习能力只能想到这一步,先根据固定的条件进行查询筛选,然后再根据用户id进行分组,查询每个用户的投资总金额,但是如何使最后结果能返回符合所有条件的用户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"}
}
}
}
}
}
{
"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"}
}
}
}
}
}
ElasticSearch入门
67740 学习 · 302 问题
相似问题
回答 2
回答 1