pyes范围查询语法

时间:2012-08-15 03:30:34

标签: python elasticsearch pyes

我正在尝试将下面的elasticsearch转换为pyes查询,但我找不到有关如何使用范围查询的示例(尤其是时间戳)。有人可以帮忙吗?

Elasticsearch Query

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": "abcd"
          }
        },
        {
          "query_string": {
            "default_field": "@field2",
            "query": "efgh"
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": "2012-08-14T20:45:09.000Z"
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "@field3": "ijkl"
          }
        }
      ]

    }
  },
  "size": 1,
}

我的Python脚本(使用pyes)来获得结果,我正在使用pyes 0.19.1

#!/usr/bin/python
# -*- coding: utf-8 -*-
from pyes import *

conn = ES('127.0.0.1:9200')


def main():
    try:
        q1= TextQuery('field1','abcd')
        q2=TextQuery('@field2','efgh')
        q3=Range('@timestamp','2012-08-14T20:45:09.000Z','2012-08-14T20:45:09.000Z')
        q4=TextQuery('@field3','ijkl')
        q = BoolQuery(must=[q1, q2,q3],must_not=[q4])
        results = conn.search(q,size='1')
        for r in results:
            print r;
    except:
        pass


if __name__ == '__main__':
    main() 

1 个答案:

答案 0 :(得分:4)

尝试用{/ 1>替换Range

RangeQuery(qrange=ESRange('@timestamp',from_value='2012-08-14T20:45:09.000Z',to_value='2012-08-14T20:45:09.000Z'))

我相信只要您的时间戳值与映射中的格式匹配,它就会起作用。