gpt4 book ai didi

python - Python中Elasticsearch相对时间范围查询

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:55 25 4
gpt4 key购买 nike

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

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

足够简单吧?那么有没有办法在此查询中包含相对时间范围,或者我是否需要编写一些Python来计算出要作为时间范围插入的时间?

预先感谢您的帮助!

最佳答案

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

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

定义基于时间的 key :

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

关于python - Python中Elasticsearch相对时间范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29797331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com