How to access the documents that is retrieved from the search aggregation nest bucket

I have a query like this -

var result = clientConnection.Search<dynamic>(s => s
    .Index(indexname)
    .Type(typename)
.Aggregations(a => a
    .DateRange(fieldvalue, d => d
        .Field(fieldname)

        .Ranges(
            r => r.To("2016-03-30T19:40:50+00:00"),
            r => r.From("2016-03-15T19:40:50+00:00")
        )
    )
)
);

           var agg = result.Aggs.DateRange("L2");

But this gives me 2 nest buckets like this

This gives me only the document count. But I need to see the values inside the document. How do I retrieve the values in the document ? I used Nest.BucketItem but I am not able to retrieve it.

Also I can see that there are 2 buckets. Why not 1? Please suggest

I answered your question on Stack Overflow; I'll include here too :smile:

If you're after the actual documents, then a query to get the documents is probably what you need, as opposed to an aggregation. You can issue multiple queries in one request with Multi Search API

var indexname = "index-name";
var typename = "type-name";
var fieldname = "field-name";

var result = client.MultiSearch(ms => ms        
    .Index(indexname)
    .Type(typename)
    .Search<dynamic>("search1", s => s
        .Query(q => +q
            .DateRange(r => r
                .Field(fieldname)
                .LessThan("2016-03-30T19:40:50+00:00")
            )
        )
    )
    .Search<dynamic>("search2", s => s
        .Query(q => +q
            .DateRange(r => r
                .Field(fieldname)
                .GreaterThan("2016-03-15T19:40:50+00:00")
            )
        )
    )
);

var search1Documents = result.GetResponse<dynamic>("search1").Documents;   
var search2Documents = result.GetResponse<dynamic>("search2").Documents;

This will return you the first 10 documents that match, for each of the queries. You can use .From() and .Size() on each search to paginate results, and if you need to fetch a lot of documents, consider using the Scroll API.

Hi ross I am using nest version 1.4 . Could you tell me how do use that by keeping the nest version same as 1.4

Hi Ross could you please have a look over this ?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.