【问题标题】:Terms Aggregations of fields术语字段聚合
【发布时间】:2020-01-10 10:55:15
【问题描述】:

我正在从指标中读取数据,并希望在最新日期时间读取选定的数据。我使用聚合来获得第一名,但由于查询给出的结果与最新的不匹配而在某处失败。可能是我缺少什么地方。这是我的最终查询

var elasticResponse = elasticClient.Search<object>(s => s
                .Aggregations(ag => ag
                    .Terms("cpu_aggs", sa => sa.Field("system.cpu.total.pct").Size(1))
                    .Terms("memory_aggs", sa => sa.Field("system.memory.actual.used.pct").Size(1))
                    .Terms("diskio_aggs", sa => sa.Field("docker.diskio.summary.bytes").Size(1))
                    .Terms("load_aggs", sa => sa.Field("system.load.5").Size(1))
                )
                .Sort(so => so.Descending("@timestamp"))
            );

请帮助找到正确的解决方案。

更新的解决方案是

.Terms(currentMemoryUsageInPercent, sa => sa.Field("system.memory.actual.used.pct").Size(1).Order(o => o.Descending("max_timestamp"))
                .Aggregations(agg => agg.Max("max_timestamp", mx => mx.Field(greoupByFieldName))))

【问题讨论】:

    标签: elasticsearch dsl querydsl


    【解决方案1】:

    你想达到什么目标?

    聚合是一个groupby

    .Aggregations(ag => ag
                         .Terms("cpu_aggs", sa => sa.Field("system.cpu.total.pct").Size(1))
                         .Terms("memory_aggs", sa => sa.Field("system.memory.actual.used.pct").Size(1))
                         .Terms("diskio_aggs", sa => sa.Field("docker.diskio.summary.bytes").Size(1))
                         .Terms("load_aggs", sa => sa.Field("system.load.5").Size(1))
                     )
    
    
    
    
    .Terms("cpu_aggs", sa => sa.Field("system.cpu.total.pct").Size(1)
    
    
    
     Means you want the most (and only because of the size(1) system.cpu.total.pct in your data 
        then
    

    .Terms("memory_aggs", sa =&gt; sa.Field("system.memory.actual.used.pct").Size(1)) => 最多 system.memory.actual.used.pct 值 等等

    All these aggregation are at the same level, so you will have most reccurent pct, most recurent memory, more recurent bytes ect...Each of these aggregations is independant and will scan all your document.
    
    
    
        Your sort is not apply to aggregation, it will be apply to your query, by default, 10 documents are returned, so it will return the last 10 documents.
    

    进行聚合以分析不同的文档(递归、平均值、最大值、计数、总和...)。

    例如,你可以做什么:

    首先,如果你只想使用聚合,不要要求文档,放一个 .Size(0)。

    var elasticResponse = Client.Search<object>(s => s
                    .Size(0)
                     .Aggregations(ag => ag
    

    如果你想对一个字段进行聚合,以获取更多的 10 个重复值:

    .Terms("cpu_aggs", sa => sa.Field("system.cpu.total.pct").Size(10)
    

    如果您想要“system.cpu.total.pct”的 10 个以上的重复次数以及每个人的最后一个文档

    .Terms("cpu_aggs", sa => sa.Field("system.cpu.total.pct")).TopHits("topcpu", r => r.Field("@timestamp").Sort(ss => ss.Descending()).Size(1)))
    

    如果您想要 10 个更频繁的“cpu_aggs”,并且对于每个顶级 cpu 值,5 顶“system.load.5”

    .Terms("cpu_aggs", sa => sa.Field("docker.diskio.summary.bytes").Size(10)
                            .Aggregations(subag => subag.Terms("subaggr", sua => sua.Field("system.load.5").Size(5))
                         ))
    

    如果你想要统计信息(例如 system.cpu.total.pct 是一个数字)

    .Stats("cpu_stats", sa => sa.Field("system.cpu.total.pct"))
    

    如果只想要最后一个文档,则不需要聚合:

    var elasticResponse = elasticClient.Search<object>(s => s
                    .Size(1)
                    .Sort(so => so.Descending("@timestamp"))
                );
    

    如果这不能回答您的问题,请澄清,并加入一些您需要的示例。

    【讨论】:

    • 感谢 LeBigCat 的宝贵时间和建议。
    • 实际上我想要所有文档中所选指标的最新值,例如“system.memory.actual.used.pct”等。我想通过聚合来获得它。建议最好的方法是什么?
    • 您提供的解决方案.Terms("cpu_aggs", sa =&gt; sa.Field("system.cpu.total.pct")).TopHits("topcpu", r =&gt; r.Field("@timestamp").Sort(ss =&gt; ss.Descending()).Size(1))) 给出错误 - elasticsearch parsing_exception unknown key for a value_string in [field]
    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 2014-05-12
    • 2018-10-18
    • 2016-01-22
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多