duanshan188866 2017-08-30 23:43
浏览 1095

如何按时间范围过滤Go中的Elasticsearch结果?

I'm using olivere's v.5 elasticsearch library - https://godoc.org/github.com/olivere/elastic

Trying to do something similar to this post which uses the v.2 library - How to search in elasticsearch with Go filtering results by time frames

But 'NewRangeFilter' and 'NewFilteredQuery' are not available in v.5. There is a 'DateRange' API in v.5 (https://godoc.org/github.com/olivere/elastic#Aggregations.DateRange) that I can call from an Aggregation, but it takes in a string, so I don't know what I'm supposed to pass in.

This is what I've tried so far to build an aggregation. After that, I'm not sure what to pass into the DateRange function. I have an index called 'tmpindex' and type called 'user' and each document has a 'timestamp' property which is an integer.

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
    }
  • 写回答

1条回答 默认 最新

  • dongyishen5796 2017-08-30 23:53
    关注

    I think you're looking for Range Query.

    You'd use it something like this...

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

    Where start and end are time.Time values and "timestamp" is the name of your time field.

    I should note that wrapping it in a filter of a Bool Query is just one way to use it. It can be used anywhere you can pass a Query.

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员