1

我正在尝试使用 NEST 实现 ElasticSearch 查询,以便能够生成键/值对的结果,其中键是日期,值是计数。我要查询的文档都有一个 created_date ,我想计算过去 7 天或类似时间内每天插入的文档数量。

我检查了 IElasticClient 的 Count 方法,但这似乎给了我一个总数,而不是每天。我想我需要对数据做一个方面,但不能完全弄清楚如何实现它。

任何帮助,将不胜感激 :)

4

2 回答 2

1

你想要的是一个date_histogram-facet。

这是一个示例,应该涵盖我可以解释您想要的各种方式:

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type","_id":1}}
{"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
{"index":{"_index":"play","_type":"type","_id":2}}
{"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
{"index":{"_index":"play","_type":"type","_id":3}}
{"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
{"index":{"_index":"play","_type":"type","_id":4}}
{"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
'


# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "facets": {
        "foos_per_interval": {
            "date_histogram": {
                "key_field": "created_date",
                "value_field": "count",
                "interval": "day"
            },
            "facet_filter": {
                "term": {
                    "key": "foo"
                }
            }
        }
    }
}
'

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "play",
      "_type" : "type",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"created_date":"2013-09-30T12:00:00Z","key":"foo","count":12}
    }, {
      "_index" : "play",
      "_type" : "type",
      "_id" : "2",
      "_score" : 1.0, "_source" : {"created_date":"2013-09-30T13:00:00Z","key":"bar","count":14}
    }, {
      "_index" : "play",
      "_type" : "type",
      "_id" : "3",
      "_score" : 1.0, "_source" : {"created_date":"2013-10-01T12:00:00Z","key":"foo","count":42}
    }, {
      "_index" : "play",
      "_type" : "type",
      "_id" : "4",
      "_score" : 1.0, "_source" : {"created_date":"2013-10-01T14:00:00Z","key":"foo","count":13}
    } ]
  },
  "facets" : {
    "foos_per_interval" : {
      "_type" : "date_histogram",
      "entries" : [ {
        "time" : 1380499200000,
        "count" : 1,
        "min" : 12.0,
        "max" : 12.0,
        "total" : 12.0,
        "total_count" : 1,
        "mean" : 12.0
      }, {
        "time" : 1380585600000,
        "count" : 2,
        "min" : 13.0,
        "max" : 42.0,
        "total" : 55.0,
        "total_count" : 2,
        "mean" : 27.5
      } ]
    }
  }
}
于 2013-10-01T18:10:13.107 回答
1

使用方面确实是要走的路:

http://www.elasticsearch.org/guide/reference/api/search/facets/date-histogram-facet/

public class Doc
{
    public string Id { get; set; }
    public DateTime CreatedOn { get; set; }
}

public void TempFacetExample()
{
    var result = this._client.Search<Doc>(s => s
        .FacetDateHistogram(fd => fd
            .OnField(p => p.CreatedOn)
            .Interval(DateInterval.Day)
            //global forces it to count out of scope
            //from the main query (if any)
            .Global()
            .FacetFilter(ff => ff
                .Range(rf => rf
                    .From(DateTime.UtcNow.AddDays(-7))
                    .To(DateTime.UtcNow)
                )
            )
        )
    );
    var facetBucket = result.Facet<DateHistogramFacet>(p => p.CreatedOn);
    //facetBucket.Items now holds the days with counts
    //if I remember correctly elasticsearch wont return empty buckets
    //so you have to handle missing days (that have no docs).

}
于 2013-10-01T18:15:23.417 回答