2016-06-21 11 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": [] 
        } 
       } 
      ] 
     } 
     } 
    } 
} 

私はまた、次のクエリでmanufacType 1持っているだけでoemItemsを見て、ネストされた用語のフィルタを追加しようとしました。ただし、oemItemsにmanufacType 1が含まれているオブジェクトを返します。つまり、製品内のoemItemsには1または0のmanufacTypeが含まれています。私は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

良いスタートです、この応答に集計を行っていることだけoemItems.manufactorNameを返しますどのように表示されません。

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のバケットは空です –