2015-11-05 96 views
1

在elasticsearch中,我可以聚合第二个聚合的数字字段上的聚合。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排序a1:“a”,然后FIELD2:“b”,然后FIELD2: “C”。

就我而言,每个FIELD1都有一个唯一的 FIELD2。所以我真的想要一种方法来排序FIELD1的a1结果。

+0

您是否尝试使用与上述查询相同的方法。 ? – piyushGoyal

回答

1

我不确定你到底想要什么,但我试着跟着。 我创建映射索引

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 
        } 
        ] 
       } 
      } 
     ] 
     } 
    } 

确保您的FIELD2not_analyzed或者您将得到意想不到的结果。

这有帮助吗?

+0

他试图实现的是根据第二次聚合的结果对第一个聚合进行排序(内部聚合)。这里的扭曲是第二次聚合是字符上的术语,并且桶结果是字符串值而不是整数值。 – piyushGoyal

+0

如果一个内部聚合是一个多值桶,那么就不可能对子agg上的父级聚合进行排序。 – piyushGoyal

+0

是的,@piyushGoyal是正确的。在我的情况下,内部聚合从来不是一个多值桶,所以我应该能够以某种方式按agg2值对agg1值进行排序。 – maia

1

我发现了一种可行的方法。您必须首先在FIELD1上汇总FIELD1,,然后

{ 
     "size": 0, 
     "aggs": { 
     "a2": { 
      "terms": { 
      "size": 0, 
      "field": "FIELD2", 
      "order": { 
       "_term": "asc" 
      } 
      }, 
      "aggs": { 
      "a1": { 
       "terms": { 
       "size": 0, 
       "field": "FIELD1", 
       "order": { 
        "_term": "asc" 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
+0

这与你在问题中提出的问题是相反的吗? – ChintanShah25

+0

@ ChintanShah25我的目标是排序FIELD1聚合通过这种方式执行,FIELD1被FIELD2排序(尽管以嵌套的方式)。 – maia