Richard Barraclough
Richard Barraclough

Reputation: 2964

NEST (ElasticSearch) search response drops aggregates

Here is a query that works in ElasticSearch.

   "query":{
      "match_all":{

      }
   },
   "size":20,
   "aggs":{
      "CompanyName.raw":{
         "terms":{
            "field":"CompanyName.raw",
            "size":20,
            "order":{
               "_count":"desc"
            }
         }
      }
   }
}

The response from ElasticSearch has a property aggregations['CompanyName.raw']['buckets'] which is an array.

I use this code to exeute the same query via NEST

                string responseJson = null;
                ISearchResponse<ProductPurchasing> r =  Client.Search<ProductPurchasing>(rq);
                using (MemoryStream ms = new MemoryStream())
                {
                    Client.RequestResponseSerializer.Serialize<ISearchResponse<ProductPurchasing>>(r, ms);
                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        responseJson = sr.ReadToEnd();
                    }
                }

However, in the resulting responseJson this array is always empty.

Where hs it gone? How can I get it back? Or is it that NEST doesn't support aggregates?

Upvotes: 1

Views: 952

Answers (1)

Rob
Rob

Reputation: 9979

NEST does support aggregation, you can have a look into docs on how to handle aggregation response with NEST help.

Here you can find a short example of writing and retrieving data from simple terms aggregation:

class Program
{
    public class Document
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Brand { get; set; }
        public string Category { get; set; }

        public override string ToString() => $"Id: {Id} Name: {Name} Brand: {Brand} Category: {Category}";
    }

    static async Task Main(string[] args)
    {
        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
        var connectionSettings = new ConnectionSettings(pool);
        connectionSettings.DefaultIndex("documents");

        var client = new ElasticClient(connectionSettings);

        var deleteIndexResponse = await client.Indices.DeleteAsync("documents");
        var createIndexResponse = await client.Indices.CreateAsync("documents", d => d
            .Map(m => m.AutoMap<Document>()));

        var indexDocument = await client
            .IndexDocumentAsync(new Document {Id = 1, Brand = "Tommy", Category = "men"});
        var indexDocument2 = await client
            .IndexDocumentAsync(new Document {Id = 2, Brand = "Diesel", Category = "men"});
        var indexDocument3 = await client
            .IndexDocumentAsync(new Document {Id = 3, Brand = "Boss", Category = "men"});

        var refreshAsync = client.Indices.RefreshAsync();

        var searchResponse = await client.SearchAsync<Document>(s => s
            .Query(q => q.MatchAll())
            .Aggregations(a => a
                .Terms("brand", t => t
                    .Field(f => f.Brand.Suffix("keyword")))));

        var brands = searchResponse.Aggregations.Terms("brand");
        foreach (var bucket in brands.Buckets)
        {
            Console.WriteLine(bucket.Key);
        }
    }
}

Prints:

Boss
Diesel
Tommy

Hope that helps.

Upvotes: 0

Related Questions