从具有匹配模式的每个索引中提取最后索引的文档。

huangapple go评论51阅读模式
英文:

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"}
            }]
          }
        }
      }
    }
  }
}

huangapple
  • 本文由 发表于 2023年3月3日 18:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定