带有日期数学的Elasticsearch query_string

时间:2017-08-25 20:04:04

标签: elasticsearch elasticsearch-5

我试图在Elasticsearch中使用日期数学进行日期范围查询,但它没有返回任何结果:

`query: {
    bool: {
        must: {
            query_string: {
                query: "created_at:[now-1m/d TO now/d+1d]", default_operator: "AND", analyzer: "my_text"}
            }
        }
    }
}`

更改查询以包含实际日期确实有效:" created_at:[2017-07-25至2017-08-25]"

我还尝试将query_string与范围查询相结合,但是没有返回任何结果:

`query: {
    bool: {
        must: [
            {query_string: {query: "", default_operator: "AND", analyzer: "my_text"}}, 
            {range: {"created_at"=>{from: "now-1m/d", to: "now/d+1d"}}}]
        }
    }
}`

使用日期数学确实会在aggs中显示正确的计数:

`aggs: {
    created_at: {
        date_range: {
            field: "created_at", keyed: true, ranges: [
                {from: "now/d", to: "now/d+1d", key: "Today"}, 
                {from: "now-1d/d", to: "now/d+1d", key: "In the last day"}, 
                {from: "now-7d/d", to: "now/d+1d", key: "In the last week"}, 
                {from: "now-1M/d", to: "now/d+1d", key: "In the last month"}
            ]
        }
    }
}`

日期数学是否适用于Elasticsearch query_string查询?如果不是什么是正确的解决方法?

1 个答案:

答案 0 :(得分:1)

我认为您有大写字母错误 - m应为M[now-1M/d TO now/d+1d](大写字母在您的上一个示例中是正确的,这就是它在那里工作的原因)。