使用NEST过滤子聚合中的聚合结果

时间:2015-06-12 12:28:56

标签: c# elasticsearch aggregation nest

我有生产清单,希望在两个不同的时间段内获得每种产品的销售数量。能够生成弹性搜索查询,如下所示,

POST /Elastic/products/_search
{
  "size": 0,
  "query": {
    "query_string": {
      "query": "(date:[20130501 TO 20140430])"
    }
  },
  "aggs": {
    "last12months": {
      "terms": {
        "field": "product",
        "order": {
          "last3months": "desc"
        },
        "exclude": "NA",
        "size": 20
      },
      "aggs": {
        "last3months": {
          "filter": {
            "range": {
              "date": {
                "gte": "20140201",
                "lte": "20140430"
              }
            }
          }
        }
      }
    }
  }
}

结果如下:

"aggregations": {
      "last12months": {
         "doc_count_error_upper_bound": -1,
         "sum_other_doc_count": 938,
         "buckets": [
            {
               "key": "xxxx",
               "doc_count": 55,
               "last3months": {
                  "doc_count": 41
               }
            },
            {
               "key": "yyyy",
               "doc_count": 41,
               "last3months": {
                  "doc_count": 14
               }
            }
         ]
      }
   }

由于我使用nest.net从我的应用程序生成查询并能够生成查询,但是子聚合部分在这里有点棘手。无法在子聚合上应用范围内过滤器。

ElasticSearchClient.Search<Product>(s => s.Size(0)
                .Query(query => query
                    .Filtered(filtered => filtered
                        .Filter(filter => filter
                            .Range(range => range
                                .OnField(ElasticFields.JobDate)
                                .GreaterOrEquals("20140101")
                                .LowerOrEquals("20141231"))
                        .Query(q => q
                            .QueryString(qs => qs.Query(queryString)))))
                .Aggregations(aggregation => aggregation
                    .Terms("last12months", t1 => t1

                        .Field("product")
                        .OrderDescending("last3months")
                        .Size(25)
                        .Aggregations(innerAggregation => innerAggregation.Filter("last3months", t2 => t2.Filters(t3 => t3.Range(range => range.LowerOrEquals("").GreaterOrEquals("")))))
                        )));

如何实现这个目标?

非常感谢

0 个答案:

没有答案