将Elasticsearch JSON查询转换为C#NEST

时间:2017-06-29 14:02:45

标签: c# elasticsearch nest

我有一个Elasticsearch数据库,我有一个索引test

这里的架构:

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "channel" : {
            "properties" : {
                "id" : { "type" : "integer" },
                "name" : { "type" : "string" }
            }
        },
        "segment" : {
            "properties" : {
                "groupid" : { "type" : "text", "fielddata": true  },
                "instrName" : { "type" : "text", "fielddata": true },
                "channelList" : { "type" : "object" }
            }
        }
    }
}

我想将此查询转换为C#NEST代码:

 GET /test/segment/_search
    {
      "aggs": {
        "agg": {
          "terms": {
            "field": "instrName"
          },
          "aggs": {
            "agg2": {
              "terms": {
                "field": "groupid"
              }
            }
          }
        }
      }
}

我知道如何转换单个聚合查询而不是嵌套聚合

修改

这是当前的代码,但我从ES

得到500错误
    var res = elastic.Search<SegmentRecord>(
        s => s.Index(esIndex).Aggregations(a => a.Terms("instrName", x => x.Aggregations(w => w.Terms("groupid", sel => sel)))));

1 个答案:

答案 0 :(得分:1)

您尚未指定每个terms aggregation应运行的字段。具有terms子聚合的terms聚合看起来像

var res = elastic.Search<SegmentRecord>(s => s
    .Index(esIndex)
    .Aggregations(a => a
        .Terms("agg", t => t
            .Field("instrName")
            .Aggregations(sa => sa
                .Terms("agg2", tt => tt
                    .Field("groupid")
                )
            )
        )
    )
);