1
votes

I'm trying Nest plugin for querying elastic search data. I have a yearly job count report based on a field. Currently I have used the date Histogram report for this and below is the elastic query.

POST insight/_search
{
  "size": "0",
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "(onet.familycode: 11)"
        }
      }
    }
  },
  "aggregations": {
    "jobcounts_by_year": {
      "date_histogram": {
        "field": "jobdate",
        "interval": "year",
        "format": "yyyy"
      },
      "aggregations": {
        "count_by_occupation_family": {
          "terms": {
            "field": "onet.family"
          }
        }
      }
    }
  }
}

Equivalent Nest query

result = ElasticSearchClient.Instance.Search<Job>(s => s.Size(0)
                    .Query(query => query
                        .Filtered(filtered => filtered
                            .Query(q => q
                                .QueryString(qs => qs.Query(queryString)))))
                    .Aggregations(a => a
                        .DateHistogram("jobcounts_by_year", dt => dt
                            .Field(ElasticFields.JobDate)
                            .Interval("year")
                            .Format("yyyy")
                            .Aggregations(a1 => a1
                            .Terms("top_agg", t => t
                                .Field(criteria.GroupBy.GetElaticSearchTerm())
                                    .Exclude("NA|Unknown|Not available")
                                .Size(Constants.DataSizeToCompare)))
                         )));

Everything works well, but now the problem is iterating over the result to get values, For normal aggregation I'm currently doing it like below

data = result.Aggs.Terms("top_agg").Items.Select(item =>
                new JobReportResult
                {
                    Group = item.Key,
                    Count = item.DocCount
                }).ToList();

But it seems Nest doesn't support buckets with in Date Histogram buckets.

If i tried like below I'm getting null reference exception.

result.Aggs.DateHistogram("jobcounts_by_year").Terms("top_agg")

It seems we have to use something like below.The d2 now has IAggregation

    var d1 = result.Aggs.DateHistogram("jobcounts_by_year").Items;
    var d2 =(TermsAggregator)d1[0].Aggregations["top_agg"];

But the Aggregation property is not exposing any values.

I'm stuck here. Can someone let me know how can I access buckets inside DateHistogram Buckets using NEST

Regards,

1

1 Answers

1
votes

Try this

var dateHistogram = searchResponse.Aggs.DateHistogram("jobcounts_by_year");

foreach (var item in dateHistogram.Items)
{
    var bucket = item.Terms("top_agg");
}

Hope this helps.