2016-06-21 107 views
0

我想聚合具有內部對象的數據。例如:ElasticSearch 1x - 對象條件下的聚合

{ 
    "_index": "product_index-en", 
    "_type": "elasticproductmodel", 
    "_id": "000001111", 
    "_score": 6.3316255, 
    "_source": { 
     "productId": "11111111111", 
     "productIdOnlyLetterAndDigit": "11111111111", 
     "productIdOnlyDigit": "11111111111", 
     "productNumber": "11111111111", 
     "name": "Glow Plug", 
     "nameOnlyLetterAndDigit": "glowplug", 
     "productImageLarge": "11111111111.jpg", 
     "itemGroupId": "11111", 
     "relatedProductIds": [], 
     "dataAreaCountries": [ 
      "fra", 
      "pol", 
      "uk", 
      "sie", 
      "sve", 
      "atl", 
      "ita", 
      "hol", 
      "dk" 
     ], 
     "oemItems": [ 
      { 
       "manufactorName": "BERU", 
       "manufacType": "0" 
      }, 
      { 
       "manufactorName": "LUCAS", 
       "manufacType": "0" 
      } 
     ] 
    } 
} 

我需要能夠聚集oemItems.manufactorName值,但只有在oemItems.manufacType爲「0」。我已經嘗試了一些例子,比如這裏接受的例子(Elastic Search Aggregate into buckets on conditions),但我似乎無法將它包裹在頭上。

我試過下面,希望它會首先在manufacType上進行加密,然後再對它進行加工,然後對每種類型使用manufactorName,它似乎顯示正確的命中數。然而,對於manufactorName桶是空的:

GET /product_index-en/_search 
{ 
"size": 0, 
    "aggs": { 
    "baked_goods": { 
     "nested": { 
     "path": "oemItems" 
     }, 
     "aggs": { 
     "test1": { 
      "terms": { 
      "field": "oemItems.manufacType", 
      "size": 500 
      }, 
      "aggs": { 
      "test2": { 
       "terms": { 
       "field": "oemItems.manufactorName", 
       "size": 500 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

而結果:

{ 
    "took": 27, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 471214, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "baked_goods": { 
     "doc_count": 677246, 
     "test1": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
       { 
        "key": "0", 
        "doc_count": 436557, 
        "test2": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [] 
        } 
       }, 
       { 
        "key": "1", 
        "doc_count": 240689, 
        "test2": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [] 
        } 
       } 
      ] 
     } 
     } 
    } 
} 

我也嘗試添加一個嵌套項過濾器,只查找oemItems具有manufacType 1以下查詢。但是,它返回oemItem包含manufacType 1的對象,這意味着產品中的oemItem仍包含1或0 manufacType。我看不出在這個響應做一個彙總只會返回oemItems.manufactorName其中oemItems.manufacType是0

GET /product_index-en/_search 
{ 
     "query" : { "match_all" : {} }, 
     "filter" : { 
      "nested" : { 
       "path" : "oemItems", 
       "filter" : { 
        "bool" : { 
         "must" : [ 
          { 
           "term" : {"oemItems.manufacType" : "1"} 
          } 
         ] 
        } 
       } 
      } 
     }  
} 
+0

首先,你需要確保'oemItems'在你的映射中是'nested'類型的。是這樣嗎? – Val

+0

@Val不,它不是嵌套類型。我會改變它,看看是否有幫助。 –

+0

@Val我將它設置爲嵌套並在我的文章中添加了一個示例。 –

回答

1

良好的開端至今。試試這樣:

POST /product_index-en/_search 
{ 
    "size": 0, 
    "query": { 
    "nested": { 
     "path": "oemItems", 
     "query": { 
      "term": { 
       "oemItems.manufacType": "0" 
      } 
     } 
    } 
    }, 
    "aggs": { 
    "baked_goods": { 
     "nested": { 
     "path": "oemItems" 
     }, 
     "aggs": { 
     "test1": { 
      "terms": { 
      "field": "oemItems.manufactorName", 
      "size": 500 
      } 
     } 
     } 
    } 
    } 
} 
+0

問題是Object.oemItems可能包含具有manufacType 1,0或多個的對象。因此查詢返回的匹配將包括除了0之外還具有manufactorType 1的對象,並且當我彙總這些結果時,我最終得到了manufacType 1和0.我想我需要向聚合添加一個過濾器,所以它只返回manufacType 0的oemItems? –

+0

試一試,它應該工作,因爲嵌套字段是下面的不同文檔。 – Val

+0

感謝您的幫助Val。我做了,但test1中的桶是空的 –