简体   繁体   English

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

[英]Elasticsearch relative time range query in Python

I have searched and searched but cannot find an answer for this. 我已经搜索了,但是找不到答案。 I am new to using Elasticsearch with Python and trying to do a simple Python query against my Elasticsearch index which will return a count of the results matching a specific set of criteria in the past hour. 我刚开始使用Elasticsearch和Python,并尝试对我的Elasticsearch索引执行简单的Python查询,该查询将在过去一个小时内返回与一组特定条件匹配的结果计数。 I'm getting all the results back using the following (sanitized) code: 我使用以下(经过消毒的)代码获取所有结果:

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

Simple enough right? 很简单吧? So is there a way to include a relative time range in this query, or do I need to write some Python to figure out the times to insert as a time range? 那么有没有办法在此查询中包含一个相对时间范围,还是我需要编写一些Python来确定作为时间范围插入的时间?

Thanks in advance for the help! 先谢谢您的帮助!

Yes, everything you need is a time-based key in your index and then query that key with: 是的,您需要的只是索引中基于时间的键,然后使用以下命令查询该键:

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

To define your time-based key: 定义基于时间的密钥:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM