【问题标题】:Elasticsearch query_string with date math带有日期数学的 Elasticsearch query_string
【发布时间】:2017-08-25 20:04:03
【问题描述】:

我正在尝试在 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 TO 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 查询?如果不是,那么正确的解决方法是什么?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    我认为你有一个大写错误 -- m 应该是 M: [now-1M/d TO now/d+1d] (你的最后一个例子中的大写是正确的,这就是它在那里工作的原因)。

    【讨论】:

    • 您是对的,但即使我将其更改为 M 它仍然返回 0 结果。 query: {query_string: {query: "created_at:[now-1M/d TO now/d+1d]", default_operator: "AND", analyzer: "my_text"}.... 30d 和 1Y 选项都有效,所以我猜月日期数学存在一些问题。这两个都有效:“created_at:[now-30d/d TO now/d+1d]”、“created_at:[now-1Yd/d TO now/d+1d]”
    猜你喜欢
    • 2022-03-08
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2020-08-31
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多