Elasticsearch-dsl-py: How to create range filter

Created on 15 Dec 2014  路  6Comments  路  Source: elastic/elasticsearch-dsl-py

I want this:

"range": {
          "@timestamp": {
            "gte": "2014-12-3T16:00:00",
            "lte": "now"
          }
  }

but this code does different thing:

s = Search( using=client, index="my_index" )
s = s.filter( 'range', gte='now-1h', lte='now', field='@timestamp' )
 "range": {
          "field": "@timestamp", 
          "gte": "now-1h", 
          "lte": "now"
 }

@timestamp is put in the wrong place, how to correct it?

Most helpful comment

Hey @garyelephant as far as i understand you are trying to implement a range filter in dsl. You can do something like this:

s.filter('range', **{field_name: {"from": from_date, "to": to_date}})

In case you are trying to input the field name and date ranges from variables.

You can also replace lte and gte instead of from and to

All 6 comments

is this the only way ?

f = F({'range':{'@timestamp':{'gte':'now-1h','lte':'now'}}})
s = s.filter( f )

You can use several ways - anything accepted by F can also be passed in to .filter(), you can also use the **{'@timestamp': {...}} notation like this:

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

Can elasticsearch-dsl create this nested structure automatically by pass simple params something like

s.filter( 'range', '@timestamp', gte='...', lte='...') 

Hey @garyelephant as far as i understand you are trying to implement a range filter in dsl. You can do something like this:

s.filter('range', **{field_name: {"from": from_date, "to": to_date}})

In case you are trying to input the field name and date ranges from variables.

You can also replace lte and gte instead of from and to

Hmm, I like this approach for simple things but I am not sure it will be still clear for more complex structure and it might lead to confusion there - for example what if the structure is even more nested - either has multiple levels or multiple keys?

The biggest advantage of the current approach, as I see it, is that it's absolutely straightforward to use based on the JSON DSL (which is described in the elasticsearch docs) and no additional knowledge is needed, this might bring in more indirection.

I will leave the ticket open for now so we can have a discussion here and decide what we should do here.

I still don't see how we could implement this consistently so I am closing the ticket.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

takaomag picture takaomag  路  3Comments

abuzakaria picture abuzakaria  路  4Comments

MauriJHN picture MauriJHN  路  4Comments

leoliuxd picture leoliuxd  路  4Comments

ypkkhatri picture ypkkhatri  路  4Comments