2015-11-05 71 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