Search code examples
pythonelasticsearchelasticsearch-dslelasticsearch-dsl-py

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("???")

Solution

  • 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'}})