2016-09-20 3 views
0

Elasticsearchはバージョン管理をサポートしていませんので、この偉大な答えのアプローチ#3を使用して自分で実装しました:https://stackoverflow.com/a/8226684/4769188各ドキュメントの最新版を取得

ここでは、日付範囲[from..to]のすべてのバージョンのすべてのバージョンを取得し、各ドキュメントの最新バージョンを1つだけ取りたいとします。これどうやってするの?

+0

あなたは#3を実装している場合は、最新のバージョンのみ別のインデックスの右側になりますか?なぜあなたは最新のものだけを気にしたら、すべてのバージョンを取得したいのですか?または、特定の日付範囲に属するすべてのバージョンを取得することを意味し、それらの可能性として古いバージョンの中で、最新のものを選択しますか? – jay

+0

@jay特定の日付範囲に属するすべてのバージョンを取得し、最新のものを選択することを意味します。 –

答えて

1

この場合に役立ちます...

を参照してください。私は、次のドキュメントをインデックス化:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 4, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "test_index", 
     "_type": "test", 
     "_id": "2", 
     "_score": 1, 
     "_source": { 
      "doc_id": 123, 
      "version": 2, 
      "text": "Foo Bar", 
      "date": "2011-09-01", 
      "current": false 
     } 
     }, 
     { 
     "_index": "test_index", 
     "_type": "test", 
     "_id": "4", 
     "_score": 1, 
     "_source": { 
      "doc_id": 123, 
      "version": 4, 
      "text": "Foo Bar", 
      "date": "2011-07-01", 
      "current": false 
     } 
     }, 
     { 
     "_index": "test_index", 
     "_type": "test", 
     "_id": "1", 
     "_score": 1, 
     "_source": { 
      "doc_id": 123, 
      "version": 1, 
      "text": "Foo Bar", 
      "date": "2011-10-01", 
      "current": true 
     } 
     }, 
     { 
     "_index": "test_index", 
     "_type": "test", 
     "_id": "3", 
     "_score": 1, 
     "_source": { 
      "doc_id": 123, 
      "version": 3, 
      "text": "Foo Bar", 
      "date": "2011-08-01", 
      "current": false 
     } 
     } 
    ] 
    }} 

は、次のクエリを使用します。これにより、ドキュメントのバージョン3が返されます。 「top_hits」内の「size」パラメータは、バケットあたりのドキュメント数を決定します。 (今は1に設定されています)。

{ 
    "size" : 0, 
    "query" : { 
     "filtered" : { 
      "query" : { 
       "match_all" : {} 
      }, 
      "filter" : { 
       "range" : { 
        "date" : { 
         "gte" : "2011-07-02", 
         "lte" : "2011-09-01" 
        } 
       } 
      } 
     } 
    }, 
    "aggs" : { 
     "doc_id_groups" : { 
      "terms" : { 
       "field" : "doc_id", 
       "size" : "10", 
       "order" : { 
        "top_score" : "desc" 
       } 
      }, 
      "aggs" : { 
       "top_score" : { 
        "max" : { 
         "script" : "_score" 
        } 
       }, 
       "docs" : { 
        "top_hits" : { 
         "size" : 1, 
         "sort" : { 
          "version" : { 
           "order" : "desc" 
          } 
         }, 
         "fields" : ["doc_id", "version", "date"] 
        } 
       } 
      } 
     } 
    } 
} 
} 

応答:

{ 
    "took": 12, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "doc_id_groups": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
      "key": 123, 
      "doc_count": 2, 
      "docs": { 
      "hits": { 
       "total": 2, 
       "max_score": null, 
       "hits": [ 
       { 
        "_index": "test_index", 
        "_type": "test", 
        "_id": "3", 
        "_score": null, 
        "fields": { 
        "date": [ 
         "2011-08-01" 
        ], 
        "doc_id": [ 
         123 
        ], 
        "version": [ 
         3 
        ] 
        }, 
        "sort": [ 
        3 
        ] 
       } 
       ] 
      } 
      }, 
      "top_score": { 
      "value": 1 
      } 
     } 
     ] 
    } 
    } 
} 
+0

ありがとう、それは動作するはずです。 { "top_score": "desc" }「 」と「top_score」集約が必要なのはなぜですか? 私はそれらがなくても期待される結果を得ています –

+0

あなたは正しいです。この種のソートは、最新バージョンの入手とは関係ありません。削除することができます。 – jay

関連する問題