简体   繁体   English

Python elasticsearch范围查询

[英]Python elasticsearch range query

I know that there are several alternative elasticsearch clients for python beyond this one .我知道除了这个之外,还有几个用于 python 的替代 elasticsearch 客户端。 However, I do not have access to those.但是,我无权访问这些。 How can I write a query that has a 'less than or equal' logic for a timestamp?如何为时间戳编写具有“小于或等于”逻辑的查询? My current way of doing this is:我目前的做法是:

query = group_id:" + gid + '" AND data_model.fields.price:' + price
less_than_time = # datetime object
data = self.es.search(index=self.es_index, q=query, size=searchsize)
hits = data['hits']['hits']
results = []
for hit in hits:
    time = datetime.strptime(hit['_source']['data_model']['utc_time'], time_format)
    dt = abs(time - less_than_time).seconds
    if dt <= 0:
        results.append(hit)

This is a really clumsy way of doing it.这是一种非常笨拙的方法。 Is there a way I can keep my query generation using strings and include a range?有没有办法可以使用字符串保留我的查询生成并包含一个范围?

I have a little script that generates a query for me.我有一个小脚本可以为我生成一个查询。 The query however is in the json notation (which I believe the client can use).然而,查询采用 json 表示法(我相信客户端可以使用)。

here's my script:这是我的脚本:

#!/usr/bin/python

from datetime import datetime
import sys

RANGE = '"range":{"@timestamp":{"gte":"%s","lt":"%s"}}'
QUERY = '{"query":{"bool":{"must":[{"prefix": {"myType":"test"}},{%s}]}}}'

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print "\nERROR: 2 Date arguments needed: From and To, for example:\n\n./range_query.py 2016-08-10T00:00:00.000Z 2016-08-10T00:00:00.000Z\n\n"
        sys.exit(1)
    try:
        date1 = datetime.strptime(sys.argv[1], "%Y-%m-%dT%H:%M:%S.%fZ")
        date2 = datetime.strptime(sys.argv[2], "%Y-%m-%dT%H:%M:%S.%fZ")

    except:
        print "\nERROR: Invalid dates. From: %s, To: %s" %(sys.argv[1], sys.argv[2]) + "\n\nValid date format: %Y-%m-%dT%H:%M:%S.%fZ\n"
        sys.exit(1)

    range_q = RANGE %(sys.argv[1], sys.argv[2])


    print(QUERY %(range_q))

The script also uses a bool query.该脚本还使用 bool 查询。 It should be fairly easy to remove that and use only the time constraints for the range.删除它应该很容易,并且只使用范围的时间限制。

I hope this is what you're looking for.我希望这就是你要找的。

This can be called and spits out a query such as:这可以被调用并吐出一个查询,例如:

./range_prefix_query.py.tmp 2016-08-10T00:00:00.000Z 2016-08-10T00:00:00.000Z
{"query":{"bool":{"must":[{"prefix": {"myType":"test"}},{"range":{"@timestamp":{"gte":"2016-08-10T00:00:00.000Z","lt":"2016-08-10T00:00:00.000Z"}}}]}}}

Artur阿图尔

Take a look at https://elasticsearch-dsl.readthedocs.io/en/latest/看看https://elasticsearch-dsl.readthedocs.io/en/latest/

        s = Search()\
            .filter("term", **{"name": name})\
            .query(q)\
            .extra(**paging)

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

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