elasticsearch - 术语字段汇总

标签 elasticsearch dsl querydsl

我正在从指标读取数据,并想在最新日期时间读取选定的数据。我使用Aggregations来获得第一名,但是由于查询给出的结果与最新不匹配而失败了。可能是我缺少某处。这是我最后的查询

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))))

最佳答案

您试图实现什么?

聚合是一个分组

.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 => 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”的十个递归与每个文件的最后一个文档一起使用
.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"))
            );

如果这不能回答您的问题,请进行说明,并加入一些您需要的示例。

关于elasticsearch - 术语字段汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59680284/

相关文章:

elasticsearch - Spring boot + Spring data Elasticsearch + Elasticsearch 5.6.0

java - 如何测量每个输入文件的 Xtend 翻译时间

java - 如何使用Querydsl的Q类?

mongodb - 是否可以通过 gradle (Kotlin-DSL) 为 Kotlin MongoDB 文档生成 Q 类?

python - 如何合并两个DSL查询以进行聚合和过滤

elasticsearch - 嵌套计数查询

spring-boot - 为什么我们需要将日志消息发送到elasticsearch?

java - 如何使用基于Xtext的语言?

Elasticsearch 查询结果过多

api - 是否有任何 REST API 查询标准/DSL 来表达 GET URL 中的复杂过滤器?