嵌套聚合搜索请求

时间:2014-06-17 14:19:01

标签: java c# database elasticsearch nest

我正在尝试通过Nest使用Elasticsearch Aggregations。首先,我在邮递员中创建我的请求,测试它们。当我看到他们正在工作时,我正试图通过Nest创建。 但我无法在nest中创建此请求:

{
"size":0,
"query":{
    "filtered":{
        "filter":{
            "term":{ "operationCode":"logincomplete"}
        }
    }
},
"aggregations" : {
    "operations": {
        "terms": {
            "field": "operationCode"
        },
        "aggs": {
            "login_count" : { "sum" : { "field" : "adet" } }
        }
    }
 }
}

目前的文档并没有说明Nest中的聚合。所以我有点卡住了。到目前为止,我已经尝试过:

var filterResults = client.Search<MCAOnlineExtRT>(s => s
            .Size(0)
            .Filter(q => q
            .Term(p => p.OperationCode, "logincomplete"))
            .Aggregations(a => a.Sum("login_count", t => t.Field(x => x.Adet)))
);

谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个:

var results = client.Search<MCAOnlineExtRT>(s => s
  .Size(0)
  .Query(q => q
    .Filtered(f => f
      .Filter(ff => ff
        .Term(o => o.OperationCode, "logincomplete"))))
  .Aggregations(a => a
    .Terms("operations", t => t.Field(o => o.OperationCode)
      .Aggregations(aa => aa
        .Sum("login_count", ss => ss.Field(o => o.Adet))))));

将产生:

{
  "size": 0,
  "aggs": {
    "operations": {
      "terms": {
        "field": "operationCode"
      },
      "aggs": {
        "login_count": {
          "sum": {
            "field": "adet"
          }
        }
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "operationCode": "logincomplete"
        }
      }
    }
  }
}

请注意流畅的API与json DSL的映射程度,它几乎是逐字逐句的。此外,aggs是ES中aggregations的简写形式。