如何访问从搜索聚合嵌套桶中检索到的文档

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

我有这样的查询:

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");

但这给了我 2 个像这样的巢桶

nest bucket

这只给我文档数。但我需要查看文档中的值。如何检索文档中的值?我使用了 Nest.BucketItem 但我无法检索它。

我还可以看到有 2 个桶。为什么不是1?请推荐

如果您追求的是实际文档,那么获取文档的查询可能就是您所需要的,而不是聚合。您可以使用 Multi Search API 在一个请求中发出多个查询。从 NEST 2.x 开始,这看起来像

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;

这将为您 return 匹配每个查询的前 10 个文档。您可以在每次搜索时使用 .From().Size() 对结果进行分页。

编辑:

使用 NEST 1.x,Multi Search 请求看起来像

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;

如果可以的话,我建议升级到最新的 5.x 版本的 Elasticsearch 和 NEST 作为 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,它应该与 Elasticsearch 1.4 兼容。