0

I have an Elasticsearch database and I have an index test

Here the schema:

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" }
            }
        }
    }
}

I'd like to convert this query into C# NEST code:

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

I know how to convert a single aggregation query but not a nested aggregation

EDIT

Here the current code but I get a 500 error from ES

    var res = elastic.Search<SegmentRecord>(
        s => s.Index(esIndex).Aggregations(a => a.Terms("instrName", x => x.Aggregations(w => w.Terms("groupid", sel => sel)))));
3
  • 1
    Can you show us your existing C# code? Commented Jun 29, 2017 at 14:14
  • Take a look at the nested aggregation usage example: elastic.co/guide/en/elasticsearch/client/net-api/current/… Commented Jun 29, 2017 at 20:59
  • @mjwills just did. please have a look, thanks Commented Jun 30, 2017 at 9:42

1 Answer 1

1

You haven't specified the field that each terms aggregation should run on. A terms aggregation with a terms sub aggregation looks like

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")
                )
            )
        )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.