2017-03-18 78 views
0

假設我有許多文件是這樣的:如何將每個文檔的數量乘以一個值?

{ 
    "foo": "Something", 
    "bars": 
    [ 
     { 
      "identifier": 1, 
      "content": "meh", 
      "quantity": 2 
     }, 
     { 
      "identifier": 2, 
      "content": "bah", 
      "quantity": 1 
     } 
    ] 
} 

我將如何查詢10個最本吧[]標識考慮公式時候,它出現*量

編輯:我取得了這樣的事情

{ 
    "size":0, 
    "aggs":{ 
     "bars":{ 
     "nested":{ 
      "path":"bars" 
     }, 
     "aggs":{ 
      "top-terms-aggregation":{ 
       "terms":{ 
        "field":"bars.identifier", 
        "size":10 
       } 
      } 
     } 
     } 
    } 
} 

但仍無法通過數量乘以領域。

+0

您可能需要一個腳本來執行此操作:https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html – Adonis

回答

1

我實現了這個使用類似這樣的查詢:

{ 
    "query": { 
     "match_all": {} 
    }, 
    "size": 0, 
    "aggs": { 
     "bars": { 
      "nested": { 
       "path": "bars" 
      }, 
      "aggs": { 
       "bars_group": { 
        "terms": { 
         "field": "bars.identifier", 
         "size": 10, 
         "order": { 
          "count_bars": "desc" 
         } 
        }, 
        "aggs": { 
         "count_bars": { 
          "sum": { 
           "field": "bars.quantity" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

我希望這可以幫助您。

相關問題