elasticsearch - NEST - 如何进行多重嵌套聚合?

标签 elasticsearch nest elasticsearch-net

如何进行多重嵌套聚合?

我尝试过这样的事情:

Aggregations(x => x
                  .Nested("Facets", y => y.Path("categories")
                    .Aggregations(r => r.Terms("categories", w => w.Field(q => q.Categories.FirstOrDefault().Id))
                  )).Nested("Facets2", s => s.Path("brand")
                    .Aggregations(e => e.Terms("brand", w => w.Field(q => q.Brand.Id))
                  )));

但它返回 Facets2 作为 Facets 的子级

有人可以帮忙吗?

最佳答案

如本示例所示,您在 NEST 客户端版本 1.7.1 上的聚合按预期工作

void Main()
{
    var settings = new ConnectionSettings();
    var connection = new InMemoryConnection(settings);
    var client = new ElasticClient(connection : connection);

    var response = client.Search<Example>(s => s
        .Aggregations(aggs => aggs
            .Nested("Facets", nested => nested
                .Path(p => p.Categories)
                .Aggregations(r => r
                    .Terms("categories", w => w
                        .Field(q => q.Categories.FirstOrDefault().Id)
                    )
                )
            )
            .Nested("Facets2", nested => nested
                .Path(p => p.Brand)
                .Aggregations(e => e
                    .Terms("brand", w => w
                        .Field(q => q.Brand.Id)
                    )
                )
            )
        )
    );

    Console.WriteLine(Encoding.UTF8.GetString(response.RequestInformation.Request));
}

public class Example
{
    public IList<Category> Categories { get; set; }
    public Brand Brand { get; set; }
}

public class Brand
{
    public int Id { get; set; }
}

public class Category
{
    public int Id { get; set; }
}

这将输出以下请求查询

{
  "aggs": {
    "Facets": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "categories": {
          "terms": {
            "field": "categories.id"
          }
        }
      }
    },
    "Facets2": {
      "nested": {
        "path": "brand"
      },
      "aggs": {
        "brand": {
          "terms": {
            "field": "brand.id"
          }
        }
      }
    }
  }
}

关于elasticsearch - NEST - 如何进行多重嵌套聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176436/

相关文章:

elasticsearch - 如何连接Play!通过Ebean到ElasticSearch的框架

elasticsearch 对唯一术语与 ngram 术语进行评分

elasticsearch - 使用Elastic Search Nest插入数据的性能问题

elasticsearch - Elasticsearch和Kibana中的集合问题

Elasticsearch 分隔有效负载 token 过滤器

elasticsearch - Logstash:查询参数低于通过Elasticsearch插件的值

elasticsearch - 每天使用NEST和ElasticSearch计数文档

elasticsearch - 有效地扫描和查找Elasticsearch索引中的敏感数据

c# - NEST Elasticsearch : how to return certain fields along with aggregated results?

elasticsearch - 一个索引的 Elasticsearch 中的多个映射