【问题标题】:How to filter Elasticsearch results in Go by time ranges?如何按时间范围过滤 Go 中的 Elasticsearch 结果?
【发布时间】:2017-08-30 23:43:27
【问题描述】:

我正在使用 Olivere 的 v.5 elasticsearch 库 - https://godoc.org/github.com/olivere/elastic

尝试做与这篇使用 v.2 库的帖子类似的事情 - How to search in elasticsearch with Go filtering results by time frames

但 'NewRangeFilter' 和 'NewFilteredQuery' 在 v.5 中不可用。 v.5 (https://godoc.org/github.com/olivere/elastic#Aggregations.DateRange) 中有一个“DateRange”API,我可以从聚合中调用它,但它需要一个字符串,所以我不知道我应该传入什么。

这是我迄今为止尝试构建聚合的方法。之后,我不确定将什么传递给 DateRange 函数。我有一个名为“tmpindex”的索引和一个名为“user”的类型,每个文档都有一个“timestamp”属性,它是一个整数。

timeline := elasticClient.NewTermsAggregation().Field("timestamp").Size(10).OrderByCountDesc()

    searchResult, err := elasticClient.Search().
        Index("tmpindex"). // search in index "tmpindex"
        Aggregation("timeline", timeline).
        From(0).Size(10).        // take documents 0-9
        Pretty(true).            // pretty print request and response JSON
        Do(context.Background()) // execute
    if err != nil {
        return err
    }

【问题讨论】:

    标签: elasticsearch go elasticsearch-5


    【解决方案1】:

    我想你正在寻找Range Query

    你会像这样使用它......

    query := elastic.NewBoolQuery().
        Filter(elastic.NewRangeQuery("timestamp").
            From(start).
            To(end))
    

    其中startendtime.Time 值,"timestamp" 是您的时间字段的名称。

    我应该注意,将它包装在 Bool 查询的过滤器中只是使用它的一种方式。它可以在任何可以传递查询的地方使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2017-06-03
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多