문제

I am trying to convert the below elasticsearch into pyes query and i could not find example on how to use the range queries (especially for time stamps). Can someone help?

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,
}

my Python script (using pyes) to get the results, i am using 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() 
도움이 되었습니까?

해결책

Try replacing that Range with

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

I believe that will work, as long as your timestamp values match the format in your mapping.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top