ElasticSearch:获取每个文档的最新版本

ElasticSearch : Get latest version of each document

我正在查询 ES 以获得某个特定时间戳内的文档列表。现在我遇到了一个场景,我们有多个版本的单个 documentId。默认情况下,ES 向我返回了该单个 documentId 的所有版本。我的要求是只获得所有文档的最后一个版本。

我还想让所有 ES 响应按一个索引时间戳字段(称为 streamingSegmentStartTime)的升序排序

我当前的查询如下所示:

{"size":25,"query":{"bool":{"must":[{"terms":{"streamingSegmentId":["00002933-be25-3b9c-9970-472b41aa53cc"],"boost":1.0}},{"range":{"streamingSegmentStartTime":{"from":1644480000000,"to":1647476658447,"include_lower":true,"include_upper":false,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["errorCount","benefitId","streamingSegmentStopTime", "fanoutPublishTimestamp", "search.version"],"excludes":[]},"sort":[{"streamingSegmentStartTime":{"order":"asc"}}, {"_timestamp": {"order": "desc"}}]}

尝试使用 collapse 参数根据字段值折叠搜索结果 https://www.elastic.co/guide/en/elasticsearch/reference/8.1/collapse-search-results.html#collapse-search-results