Search code examples
elasticsearchnestelasticsearch-6

Nest Aggregation with dynamic fields - elasticsearch


Is it possible to use nest to create buckets with keywords/fields that are not strongly typed?

Due to the nature of this project. I do not have any root objects to pass in. Below is an example.

            var result = client.Search<PortalDoc>(s => s
                        .Aggregations(a => a
                            .Terms("agg_objecttype", t => t.Field(l => "CUSTOM_FIELD_HERE"))
                        )     
                    );

Solution

  • string implicitly convert to Field, so you can pass a string for any field name

    var result = client.Search<PortalDoc>(s => s
        .Aggregations(a => a
            .Terms("agg_objecttype", t => t
                .Field("CUSTOM_FIELD_HERE")
            )
        )     
    );