Python 中的 Elasticsearch 相对时间范围查询

Elasticsearch relative time range query in Python

我找了又找,但找不到答案。我刚开始将 Elasticsearch 与 Python 结合使用,并尝试对我的 Elasticsearch 索引执行一个简单的 Python 查询,这将 return 计算过去一小时内匹配一组特定条件的结果.我正在使用以下(经过清理的)代码取回所有结果:

 hits = es.count(index='myindex-*',q=thing.rstrip() )

够简单了吧?那么有没有一种方法可以在此查询中包含相对时间范围,或者我是否需要编写一些 Python 来确定要插入的时间作为时间范围?

在此先感谢您的帮助!

是的,您需要的只是索引中基于时间的键,然后使用以下命令查询该键:

{
    "query" : {
        "range" : {
            "<time_based_key>" : {
                "gte" : "now-1h"
            }
        }
    }
}

定义基于时间的密钥:

curl -XPUT localhost:9200/<database>/<index>/_mapping?pretty -d '
{
    "<index>" : {
        "properties": {
            "<time_based_key>" : {
                "type" : "date",
                "index": "not_analyzed"
            }
        }
    }
}'