2016-12-01 6 views
0

Query the latest document of each type on Elasticsearchと同様に、私はESに一連のレコードを持っています。何私がElasticsearchは複数のフィールドでグループ化された最新のドキュメントを取得します

"size": 0, 
"aggs": { 
    "sources" : { 
     "terms" : { 
      "field" : "user" 
     }, 
     "aggs": { 
      "latest": { 
       "top_hits": { 
       "size": 1, 
       "sort": { 
        "timestamp": "desc" 
       } 
       } 
      } 
     } 
    } 
} 

:私は、ユーザーごとの最新のニュース記事」を取得することができています

"news": { 
    "properties": { 
     "source": { "type": "string", "index": "not_analyzed" }, 
     "headline": { "type": "object" }, 
     "timestamp": { "type": "date", "format": "date_hour_minute_second_millis" }, 
     "user": { "type": "string", "index": "not_analyzed" } 
     "newspaper": { "type": "string", "index": "not_analyzed"} 
    } 
} 

:例のために、それは同様ニュースだマッピングを持つ各を言うことができます達成しようとしているのは、最後の記事を1人1人、新聞とすることです。

  • ジョン、NYタイムズ、タイトル1
  • ジョン、BBC、タイトル2
  • ジェーン、NYタイムズ、TITLE3
  • など

答えて

0

あなたは別のものを追加することができますこのようなフィールドnewspapertermsサブアグリゲーション

"size": 0, 
"aggs": { 
    "sources" : { 
     "terms" : { 
      "field" : "user" 
     }, 
     "aggs": { 
      "newspaper": { 
       "terms": { 
        "field": "newspaper" 
       }, 
       "aggs": { 
        "latest": { 
        "top_hits": { 
         "size": 1, 
         "sort": { 
          "timestamp": "desc" 
         } 
        } 
        } 
       } 
      } 
     } 
    } 
} 
関連する問題