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.

    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求