2016-08-02 37 views
1

如何在elasticsearch中搜索兩個指數,這兩個指數彙總了兩個指數中出現的值?如何在兩個索引中僅對elasticsearch進行聚合?

例如:

GET indexA,indexB/_search 
{ 
    "aggs": { 
    "myField": { 
     "terms": { 
     "field": "myField" 
     } 
    } 
    } 
} 

這樣,我得到所有MyField的有兩個指標(指數A和indexB),但我怎樣才能改變這個值,這樣那隻能說明在指數A出現兩個值和indexB?爲了澄清,如果myField在indexA 中的值爲value1,value2和value3,但在indexB中只有value1和value2,我的搜索將只顯示value1和value2。

+0

要小心,默認聚合只會得到10個結果,您需要更改大小以獲取所有不同的MyField。 –

+0

這些值是哪一種類型唯一? – Richa

+0

無論如何,我認爲你不能直接與ES –

回答

1

你可以做這樣的(你需要Elasticsearch 2.X):

{ 
    "size": 0, 
    "aggs": { 
    "myField": { 
     "terms": { 
     "field": "myField" 
     }, 
     "aggs": { 
     "count_indices": { 
      "cardinality": { 
      "field": "_index" 
      } 
     }, 
     "values_bucket_filter_by_index_count": { 
      "bucket_selector": { 
      "buckets_path": { 
       "count": "count_indices" 
      }, 
      "script": "count >= 2" 
      } 
     } 
     } 
    } 
    } 
} 

隨着"terms": {"field": "myField"}你得到的唯一myField值。然後,作爲子彙總,使用"cardinality": {"field": "_index"}您可以計算具有該值的最終彙總索引的數量 - values_bucket_filter_by_index_count - 您將保留至少包含兩個索引的那些存儲桶。

到底聚集造成這個樣子:

"aggregations": { 
     "myField": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": "value1", 
       "doc_count": 2, 
       "count_indices": { 
        "value": 2 
       } 
      }, 
      { 
       "key": "value2", 
       "doc_count": 2, 
       "count_indices": { 
        "value": 2 
       } 
      } 
     ] 
     } 
    } 

正如我所說,你需要Elasticsearch x爲bucket_selector聚集。

+0

好奇......它爲你工作? –

+0

它的工作原理! :) 謝謝! –

+0

任何想法,如果我可以在kibana使用此搜索? –

相關問題