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 give 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
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. with NEST 2.x onwards, this looks like
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 each of the queries. You can use .From()
and .Size()
on each search to paginate results.
EDIT:
With NEST 1.x, the Multi Search request looks like
var indexname = "index-name";
var typename = "type-name";
var fieldname = "field-name";
var result = client.MultiSearch(ms => ms
.FixedPath(indexname, typename)
.Search<dynamic>("search1", s => s
.Query(q => q
.Filtered(fq => fq
.Filter(fqf => fqf
.Range(r => r
.OnField(fieldname)
.Lower("2016-03-30T19:40:50+00:00")
)
)
)
)
)
.Search<dynamic>("search2", s => s
.Query(q => q
.Filtered(fq => fq
.Filter(fqf => fqf
.Range(r => r
.OnField(fieldname)
.Greater("2016-03-15T19:40:50+00:00")
)
)
)
)
)
);
var search1Documents = result.GetResponse<dynamic>("search1").Documents;
var search2Documents = result.GetResponse<dynamic>("search2").Documents;
If you can though, I would recommend upgrading to the latest 5.x version of Elasticsearch and NEST as all Elasticsearch 1.x versions are now end of line for support since 16th January 2017. At the very minimum, I would recommend upgrading NEST to the latest 1.x version which should be compatible with Elasticsearch 1.4.