2015-03-03 54 views
1

我需要聚合下列文件:ElasticSearch - 聚合與文檔細節

{ 
    "title": "American Psycho", 
    "releaseDate": "7/06/2000", 
    "imdbRate": "7.6", 
    "casting": [ 
     { 
      "name": "Christian Bale", 
      "category": "Actor" 
     }, 
     { 
      "name": "Justin Theroux", 
      "category": "Actor" 
     } 
    ] 
} 

{ 
    "title": "The Dark Knight", 
    "releaseDate": "13/08/2008", 
    "imdbRate": "9.0", 
    "casting": [ 
     { 
      "name": "Christian Bale", 
      "category": "Actor" 
     }, 
     { 
      "name": "Morgan Freeman", 
      "category": "Actor" 
     } 
    ] 
} 

由演員,並希望得到以下結構:使用基於標準的長期聚集

[ 
    {"name": "Christian Bale"}, 
    {"movies": [ 
     { 
      "title": "American Psycho", 
      "releaseDate": "7/06/2000", 
      "imdbRate": "7.6" 
     }, 
     { 
      "title": "The Dark Knight", 
      "releaseDate": "13/08/2008", 
      "imdbRate": "9.0" 
     }, ... 
] 

Beyong在casting.name字段中,我如何檢索相關文檔的releaseDate和imdbRate? 對於每個演員,我還需要按照releaseDate asc排序的電影。

我可以使用單個請求執行此操作嗎?

回答

2

由於在文檔中有一個casting對象的數組,因此您需要在映射中使用嵌套類型。要獲得您想要的聚合,您需要組合Terms Aggregations,Nested AggregationsReverse Nested Aggregations。下面是一個例子。

創建和映射指數:

POST /test 
{ 
    "mappings": { 
     "movie": { 
      "properties": { 
       "title": { 
        "type": "string", 
        "fields": { 
         "raw": { 
          "type": "string", 
          "index": "not_analyzed" 
         } 
        } 
       }, 
       "releaseDate": { 
        "type": "string", 
        "index": "not_analyzed" 
       }, 
       "casting": { 
        "type": "nested", 
        "properties": { 
         "name": { 
          "type": "string", 
          "fields":{ 
          "raw": { 
           "type": "string", 
           "index": "not_analyzed" 
          } 
          } 
         }, 
         "category": { 
          "type": "string", 
          "fields":{ 
           "raw": { 
            "type": "string", 
            "index": "not_analyzed" 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

指數的文件:

POST /test/movie/1 
{ 
    "title": "American Psycho", 
    "releaseDate": "7/06/2000", 
    "imdbRate": "7.6", 
    "casting": [ 
     { 
      "name": "Christian Bale", 
      "category": "Actor" 
     }, 
     { 
      "name": "Justin Theroux", 
      "category": "Actor" 
     } 
    ] 
} 

POST /test/movie/2 
{ 
    "title": "The Dark Knight", 
    "releaseDate": "13/08/2008", 
    "imdbRate": "9.0", 
    "casting": [ 
     { 
      "name": "Christian Bale", 
      "category": "Actor" 
     }, 
     { 
      "name": "Morgan Freeman", 
      "category": "Actor" 
     } 
    ] 
} 

最後搜索:

POST /test/movie/_search?search_type=count 
{ 
    "aggs": { 
     "nested_path": { 
      "nested": { 
       "path": "casting" 
      }, 
      "aggs": { 
       "actor_name": { 
        "terms": { 
         "field": "casting.name.raw" 
        }, 
        "aggs": { 
         "movies": { 
          "reverse_nested": {}, 
          "aggs": { 
           "movie_title": { 
            "terms": { 
             "field": "title.raw" 
            }, 
            "aggs": { 
             "release_date": { 
              "terms": { 
               "field": "releaseDate" 
              } 
             }, 
             "imdbRate_date": { 
              "terms": { 
               "field": "imdbRate" 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

的克里斯蒂安·貝爾的迴應是:

{ 
    "key": "Christian Bale", 
    "doc_count": 2, 
    "movies": { 
     "doc_count": 2, 
     "movie_title": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
       { 
        "key": "American Psycho", 
        "doc_count": 1, 
        "release_date": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
          { 
           "key": "7/06/2000", 
           "doc_count": 1 
          } 
         ] 
        }, 
        "imdbRate_date": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
          { 
           "key": "7.6", 
           "doc_count": 1 
          } 
         ] 
        } 
       }, 
       { 
        "key": "The Dark Knight", 
        "doc_count": 1, 
        "release_date": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
          { 
           "key": "13/08/2008", 
           "doc_count": 1 
          } 
         ] 
        }, 
        "imdbRate_date": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
          { 
           "key": "9.0", 
           "doc_count": 1 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 
+0

好吧,所以沒有辦法避免子聚合檢索releaseDate/imdbRate,非常感謝! – julien 2015-03-03 14:13:16