gpt4 book ai didi

c# - 如何在 NEST 术语聚合查询 c# 中获得与 kibana 或 postman 一样的 Elasticsearch 结果

转载 作者:行者123 更新时间:2023-12-03 01:19:28 26 4
gpt4 key购买 nike

kibana 开发工具

GET gltrans/_search?size=0
{
"aggs": {
"code": {
"terms": {
"field": "code.keyword"
},
"aggs": {
"total": {
"sum": {
"field": "tot"
}
}
}
}
}
}

返回
 "buckets" : [
{
"key" : "0220",
"doc_count" : 30182,
"total" : {
"value" : 169779.83551708516
}
},
{
"key" : "3000",
"doc_count" : 20360,
"total" : {
"value" : -477618.27972452715
}
},................

嵌套查询
var result = _connectionToEs.EsClient().Search<gltrans>(s => s.Size(0)
.Aggregations(a => a
.Terms("code", st => st
.Field(o => o.code.Suffix("keyword"))
.Size(10)
.Aggregations(aa => aa
.Sum("total", m => m
.Field(o => o.tot))))));

没有“ key ”返回如何获得 key ?
 "Items": [
{
"tot": {
"Value": 169779.83551708516,
"ValueAsString": null,
"Meta": null
}
},
{
"tot": {
"Value": -477618.27972452715,
"ValueAsString": null,
"Meta": null
}
},

最佳答案

您可以从响应中获取 key

var result = _connectionToEs.EsClient().Search<gltrans>(s => s.Size(0)
.Aggregations(a => a
.Terms("code", st => st
.Field(o => o.code.Suffix("keyword"))
.Size(10)
.Aggregations(aa => aa
.Sum("total", m => m
.Field(o => o.tot))))));

var termsAgg = result.Aggregations.Terms("code");

foreach(var bucket in termsAgg.Buckets)
{
// get the key
var key = bucket.Key;

// get the sum aggregation for this bucket
var sumAgg = bucket.Sum("total");
}


看看 docs on handling aggregation responses .

您发布的 JSON 看起来像序列化 SearchResponse<T> 的结果。使用另一个 JSON 序列化程序,例如 JSON.NET。这不会像预期的那样工作
  • SearchResponse<T>没有被设计为再次序列化,只是通过反序列化 Elasticsearch JSON 响应来实现
  • 如果 SearchResponse<T>被设计为再次序列化,它可能无法与客户端序列化程序以外的任何其他序列化程序一起使用,因为类型可能需要特定的序列化例程。

  • 如果您想将 Elasticsearch 的逐字响应发送回调用方,有以下几种选择:

    1. 使用 DisableDirectStreaming() 捕获响应字节, 并将这些发送给调用者

    根据要求

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool).DefaultIndex("gltrans");
    var client = new ElasticClient(settings);

    var searchResponse = client.Search<gltrans>(s => s
    .RequestConfiguration(r => r
    .DisableDirectStreaming()
    )
    );

    或所有电话

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool)
    .DefaultIndex("gltrans")
    .DisableDirectStreaming();
    var client = new ElasticClient(settings);

    然后响应字节在响应中可用

    var bytes = searchResponse.ApiCall.ResponseBodyInBytes;
    var json = Encoding.UTF8.GetString(bytes);

    并不是说这会在内存中缓冲请求和响应字节,因此是以更高的内存使用和分配为代价的。



    2.使用低级客户端进行调用,返回低级响应

    var client = new ElasticClient();

    var searchRequest = new SearchDescriptor<gltrans>()
    .Query(q => q.MatchAll());

    var index = "gltrans";

    var searchResponse = client.LowLevel.Search<BytesResponse>(
    index,
    PostData.Serializable(searchRequest));

    var bytes = searchResponse.Body;

    您可以使用 client.RequestResponseSerializer如果您需要使用它,将低级别响应转换为高级别响应

    var bytes = searchResponse.Body;
    SearchResponse<gltrans> response = null;

    using (var stream = client.ConnectionSettings.MemoryStreamFactory.Create(bytes))
    {
    response = client.RequestResponseSerializer.Deserialize<SearchResponse<gltrans>>(stream);
    }

    关于c# - 如何在 NEST 术语聚合查询 c# 中获得与 kibana 或 postman 一样的 Elasticsearch 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60944111/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com