Skip to content

Instantly share code, notes, and snippets.

@timwaters
Last active December 17, 2015 08:59
Show Gist options
  • Save timwaters/5584511 to your computer and use it in GitHub Desktop.
Save timwaters/5584511 to your computer and use it in GitHub Desktop.
elasticsearch numeric_range to script filter? Seems to work?
#start_date = 2013-05-15
#"start": { "type": "date" }
date_filter = {
"or" : [
{
"numeric_range" : {
"timeframe.start" : {
"gte" : start_date,
"lte" : end_date
}
}
},
{
"numeric_range" : {
"timeframe.end" : {
"gte" : start_date,
"lte" : end_date
}
}
}
]
}
To this:
date_filter={
"or": [
{
"script": {
"script": "doc['start'].value / 1000 >= start_param && doc['start'].value / 1000 <= end_param",
"params": {
"start_param": datetime.strptime(start_date,"%Y-%m-%d").strftime("%s"),
"end_param": datetime.strptime(end_date, "%Y-%m-%d").strftime("%s")
}
}
},
{
"script": {
"script": "doc['end'].value / 1000 >= start_param && doc['end'].value / 1000 <= end_param",
"params": {
"start_param": datetime.strptime(start_date, "%Y-%m-%d").strftime("%s"),
"end_param": datetime.strptime(end_date, "%Y-%m-%d").strftime("%s")
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment