Elasticsearch不支持版本控制,所以我自己使用这个好答案的方法#3实现了它:https://stackoverflow.com/a/8226684/4769188。
现在我想要检索日期范围[from..to]的某些类型的所有版本,并且只获取每个文档的最新版本。我怎么能这样做?
答案 0 :(得分:1)
看看这是否有帮助......
我索引了以下文件:
jdb
使用以下查询。这应该返回文档的第3版。 “top_hits”中的“size”参数确定每个桶的文档数量。 (现在它设置为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
}
}
]
}}
响应:
{
"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"]
}
}
}
}
}
}
}