【问题标题】:How to read latest index in ElasticSearch如何在 ElasticSearch 中读取最新索引
【发布时间】:2020-01-02 07:35:57
【问题描述】:

在我的 ElasticSearch 中,为 metricbeat 创建了一个带有日期后缀的新索引。即“metricbeat-7.1-2019.12.20”。现在我正在尝试从最新创建的索引中读取系统参数,但我不知道该怎么做。

 var nodes = new Uri[]
            {
                new Uri(_options.Value.ElasticSearchUrl),
            };
            connectionPool = new StaticConnectionPool(nodes);
            string indexName = "metricbeat*";
            connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(indexName);
            elasticClient = new ElasticClient(connectionSettings);

            string[] systemFields = new string[]
            {
                "system.memory.*"
            };
            var elasticResponse = elasticClient.Search<object>(s => s
                .DocValueFields(dvf => dvf.Fields(systemFields)));

elasticResponse 为我提供数据,但它在时间戳中显示较旧的日期。

请提出建议。

  string[] systemFields = new string[]
            {
                "system.memory.actual.used.pct",
                "system.cpu.total.norm.pct",
                "system.load.5",
                "docker.diskio.summary.bytes"
            };
            var elasticResponse = elasticClient.Search<object>(s => s
                .DocValueFields(dvf => dvf.Fields(systemFields))
                .Aggregations(ag => ag.Max("last_process_time", sa => sa
                    .Field("@timestamp")))
                );

【问题讨论】:

  • 给出准确的索引名称,不要像上面那样使用通配符。

标签: elasticsearch nest metricbeat


【解决方案1】:

Cat indices API 将通过使用创建日期作为排序参数来帮助您找到您正在寻找的索引。

    await client.Indices.CreateAsync("metricbeat-7.1-2019.12.20", create => create);
    await client.Indices.CreateAsync("metricbeat-7.1-2019.12.21", create => create);

    var response = await client.Cat.IndicesAsync(i => i.Index("metricbeat-7.1-*")
        .Headers("index", "creation.date")
        .SortByColumns("creation.date"));

    Console.WriteLine(response.Records.LastOrDefault()?.Index);

输出:

metricbeat-7.1-2019.12.21

希望您为索引定义了ILM policy,而我的解决方案将无法检索 来自 elasticsearch 的数千个索引信息。

希望对您有所帮助。

【讨论】:

  • 感谢 Rob 的建议,我将 "sort":[ { "@timestamp": {"order": "desc"} } ] 添加到我的查询中,它运行良好,但没有找到任何方法将其添加到 NEST 查询中。我在 NEST .Sort(sh => sh.Descending(d => d.timestamp())) 中使用它,但时间戳在那里不存在。
  • 啊,我以为您正在寻找一种方法来获取可以查询的最新索引。
  • 您可以将时间戳字段名称作为字符串传递s.Sort(so =&gt; so.Descending("@timestamp"))
  • 嗨@Rob,此排序语句为我提供了具有不同时间戳的一个指标的多条记录,是的,它们已排序,但我希望每个指标仅包含最新日期一次。
  • 不是 metric beat 文档包含所有最新的指标,所以只需从响应中获取 top first 吗?
猜你喜欢
  • 2015-12-11
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多