2016-09-20 72 views
0

Elasticsearch不支持版本控制,所以我使用方法#3從這個很好的答案中實現了它:https://stackoverflow.com/a/8226684/4769188檢索每個文檔的最新版本

現在我想檢索日期範圍[from..to]的某些類型的所有版本,並且僅取每個文檔的最新版本。我怎樣才能做到這一點?

+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」中的「大小」參數決定了您想要的每個存儲桶的文檔數量。 (現在它的設置爲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