Elasticsearch Date Histogram report with Terms aggregation

荒凉一梦 提交于 2019-12-11 08:57:52

问题


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:


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.



来源:https://stackoverflow.com/questions/30666667/elasticsearch-date-histogram-report-with-terms-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!