Filter the aggregated results in sub aggregation using NEST

873 views Asked by At

I have the list of production and would like to get the sold count each product on two different time period. Able to generate the Elastic Search query as follows,

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"
              }
            }
          }
        }
      }
    }
  }
}

the result looks as follow,

"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
               }
            }
         ]
      }
   }

Since I am using nest.net to generate the query from my application and able to generate the query but subaggregation part is little tricky here. Not able to apply the range inside filter on the sub aggregation.

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

how to achieve this?

helps much appreciated

0

There are 0 answers