在elasticsearch中,我可以在第二个聚合的数值字段上聚合和排序聚合。
例如:
GET myindex/_search
{
"size":0,
"aggs": {
"a1": {
"terms": {
"field": "FIELD1",
"size":0,
"order": {"a2": "desc"}
},
"aggs":{
"a2":{
"sum":{
"field":"FIELD2"
}
}
}
}
}
}
但是,我想对分类字段值进行聚合排序。即。假设FIELD2的值是("a“、"b”、"c")之一--我想首先用FIELD2:"a“、FIELD2:"b”和FIELD2:"c“对a1进行排序。
在我的例子中,每个FIELD1都有一个唯一的FIELD2。因此,我非常希望找到一种方法来根据a1对FIELD2结果进行排序。
发布于 2015-11-04 20:39:35
我不知道你到底想要什么,但我试着跟着。我用映射创建了索引
PUT your_index
{
"mappings": {
"your_type": {
"properties": {
"name": {
"type": "string"
},
"fruit" : {"type" : "string", "index": "not_analyzed"}
}
}
}
}
然后我索引了几个像这样的文档
PUT your_index/your_type/1
{
"name" : "federer",
"fruit" : "orange"
}
然后,我用下面的聚合方法对所有的玩家进行了分类。
{
"size": 0,
"aggs": {
"a1": {
"terms": {
"field": "name",
"order": {
"_term": "asc"
}
},
"aggs": {
"a2": {
"terms": {
"field": "fruit",
"order": {
"_term": "asc"
}
}
}
}
}
}
}
我得到的结果是
"aggregations": {
"a1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "federer",
"doc_count": 3,
"a2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "green apple",
"doc_count": 1
},
{
"key": "orange",
"doc_count": 2
}
]
}
},
{
"key": "messi",
"doc_count": 2,
"a2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "apple",
"doc_count": 1
},
{
"key": "banana",
"doc_count": 1
}
]
}
},
{
"key": "nadal",
"doc_count": 2,
"a2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "blueberry",
"doc_count": 1
},
{
"key": "watermelon",
"doc_count": 1
}
]
}
},
{
"key": "ronaldo",
"doc_count": 2,
"a2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "banana",
"doc_count": 1
},
{
"key": "watermelon",
"doc_count": 1
}
]
}
}
]
}
}
确保您的FIELD2是not_analyzed
,否则会得到意想不到的结果。
这个有用吗?
发布于 2015-11-05 10:37:39
我找到了一种可行的方法。您必须首先在FIELD2上聚合,然后在上聚合,然后在FIELD1上聚合。
{
"size": 0,
"aggs": {
"a2": {
"terms": {
"size": 0,
"field": "FIELD2",
"order": {
"_term": "asc"
}
},
"aggs": {
"a1": {
"terms": {
"size": 0,
"field": "FIELD1",
"order": {
"_term": "asc"
}
}
}
}
}
}
}
https://stackoverflow.com/questions/33535012
复制相似问题