请教一个问题:
如何对聚合terms聚合后的单个桶内的文档进行相关业务查询,如对比操作等.
POST /testindex/testtype/_bulk
{ "index": {}}
{ "id" : 1, "userid" : 100, "name" : "张三", "year" : 2000 }
{ "index": {}}
{ "id" : 2, "userid" : 100, "name" : "张三", "year" : 2003 }
{ "index": {}}
{ "id" : 3, "userid" : 101, "name" : "李四", "year" : 1999 }
{ "index": {}}
{ "id" : 4, "userid" : 101, "name" : "李四", "year" : 2004 }
{ "index": {}}
{ "id" : 5, "userid" : 102, "name" : "王五", "year" : 1980 }
{ "index": {}}
{ "id" : 6, "userid" : 102, "name" : "王五", "year" : 1984 }
{ "index": {}}
{ "id" : 7, "userid" : 102, "name" : "王五", "year" : 1987 }
比如,我按照用户id分组,然后想统计桶中关于year的跨度,即相邻两条数据的对比,如张三的year跨度为3
我写了一个查询,但是发现并非我想的一样
GET testindex/_search
{
"size": 0,
"aggs": {
"byU": {
"terms": {
"field": "userid",
"size": 10,
"order": {
"_term": "asc"
}
},
"aggs": {
"sc": {
"scripted_metric": {
"init_script": "params._agg.a=[]",
"map_script": "params._agg.a.add(doc['id']);",
"reduce_script": "return params._aggs[0]"
}
}
}
}
}
}
查询出来的结果是
"aggregations": {
"byU": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 100,
"doc_count": 2,
"sc": {//这里期望为1,2
"value": {
"a": [
[
1
]
]
}
}
},
{
"key": 101,
"doc_count": 2,
"sc": {//这里期望为3,4
"value": {
"a": [
[
3
]
]
}
}
},
{
"key": 102,
"doc_count": 3,
"sc": {//这里期望为5,6,7
"value": {
"a": [
[
7
],
[
7
],
[
7
]
]
}
}
}
]
}
}
如何对聚合terms聚合后的单个桶内的文档进行相关业务查询,如对比操作等.
POST /testindex/testtype/_bulk
{ "index": {}}
{ "id" : 1, "userid" : 100, "name" : "张三", "year" : 2000 }
{ "index": {}}
{ "id" : 2, "userid" : 100, "name" : "张三", "year" : 2003 }
{ "index": {}}
{ "id" : 3, "userid" : 101, "name" : "李四", "year" : 1999 }
{ "index": {}}
{ "id" : 4, "userid" : 101, "name" : "李四", "year" : 2004 }
{ "index": {}}
{ "id" : 5, "userid" : 102, "name" : "王五", "year" : 1980 }
{ "index": {}}
{ "id" : 6, "userid" : 102, "name" : "王五", "year" : 1984 }
{ "index": {}}
{ "id" : 7, "userid" : 102, "name" : "王五", "year" : 1987 }
比如,我按照用户id分组,然后想统计桶中关于year的跨度,即相邻两条数据的对比,如张三的year跨度为3
我写了一个查询,但是发现并非我想的一样
GET testindex/_search
{
"size": 0,
"aggs": {
"byU": {
"terms": {
"field": "userid",
"size": 10,
"order": {
"_term": "asc"
}
},
"aggs": {
"sc": {
"scripted_metric": {
"init_script": "params._agg.a=[]",
"map_script": "params._agg.a.add(doc['id']);",
"reduce_script": "return params._aggs[0]"
}
}
}
}
}
}
查询出来的结果是
"aggregations": {
"byU": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 100,
"doc_count": 2,
"sc": {//这里期望为1,2
"value": {
"a": [
[
1
]
]
}
}
},
{
"key": 101,
"doc_count": 2,
"sc": {//这里期望为3,4
"value": {
"a": [
[
3
]
]
}
}
},
{
"key": 102,
"doc_count": 3,
"sc": {//这里期望为5,6,7
"value": {
"a": [
[
7
],
[
7
],
[
7
]
]
}
}
}
]
}
}
1 个回复
shiyuan
赞同来自: zx493734247