英文:
Fetch last indexed document from every index with matching pattern
问题
我有多个索引,假设如下:
test-node1
test-node2
test-node3
test-node4
test-node5
test-node6
现在我想在一次查询中从每个索引中获取最新索引文档(模式应为 test- )。
我该如何做?
在这种情况下,结果将包含总共6个文档。
英文:
I have multiple indexes let's say
test-node1
test-node2
test-node3
test-node4
test-node5
test-node6
Now i want to fetch latest indexed document from every index (pattern would be test-*) in one query.
How do i do that?
Result will contain total 6 documents in this case.
答案1
得分: 1
假设你有一个名为"timestamp"的字段,用于存储索引时间,你可以运行以下代码:
GET test-*/_search
{
"size": 0,
"aggs": {
"by_index": {
"terms": {
"field": "_index"
},
"aggs": {
"last_index": {
"top_hits": {
"size": 1,
"sort": [
{
"timestamp": {"order": "desc"}
}
]
}
}
}
}
}
}
注意:我只为代码部分提供了翻译,没有其他内容。
英文:
assuming you have a field called "timestamp" where you're storing the indexing time, you can run the following
GET test-*/_search
{
"size": 0,
"aggs": {
"by_index": {
"terms": {
"field": "_index"
},
"aggs": {
"last_index": {
"top_hits": {
"size": 1,
"sort": [{
"timestamp": {"order": "desc"}
}]
}
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论