Scott Skiles
Scott Skiles

Reputation: 3847

Query timestamp range using elasticsearch-dsl-py

I have an elasticsearch range query that I'd like to translate into elasticsearch-dsl:

Elasticsearch Python API

{"range": 
    {"@timestamp": 
        {"gte": 1570258800000, 
         "lte": 1571036400000, 
         "format": "epoch_millis"
        }
     }
 }

Elasticsearch-DSL-Py Query?

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch(<connection_details>)

s = Search(using=client, index="my-index") \
    .query("???")

Upvotes: 1

Views: 8910

Answers (3)

Prakash2403
Prakash2403

Reputation: 1

You can also supply the date in string format (YYYY-MM-DD). Here's a snippet

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch()

date_from = '2022-10-01' # Oct 01, 2022
date_to = '2022-10-05' # Oct 05, 2022

s = Search(using=client)
s = s.query('range', **{'published': {'gte': date_from, 'lte': date_to}})

Upvotes: 0

Ashish Tiwari
Ashish Tiwari

Reputation: 2277

Try this:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch(<connection_details>)

s = Search(using=client, index="my-index") \
    .filter('range' ,  **{'@timestamp': {'gte': 1570258800000 , 'lt': 1571036400000, 'format' : 'epoch_millis'}})

Upvotes: 6

Honza Kr&#225;l
Honza Kr&#225;l

Reputation: 3022

s = s.query('range', **{'@timestamp': {'gte': ...}})

Hope this helps

Upvotes: 2

Related Questions