Skip to content

Instantly share code, notes, and snippets.

@mauriciofs
Created June 6, 2017 13:52
Show Gist options
  • Save mauriciofs/99b971cc3b318ebb3aefba97cccd684c to your computer and use it in GitHub Desktop.
Save mauriciofs/99b971cc3b318ebb3aefba97cccd684c to your computer and use it in GitHub Desktop.
Elasticsearch query with date range, company id and tracker id filter
{
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"inline": "def date = LocalDateTime.ofInstant(Instant.ofEpochMilli(doc.start_date_time.value), ZoneId.of(\"+01:00\")); return Integer.parseInt(date.format(DateTimeFormatter.ofPattern(\"yyyyMMdd\")))"
},
"order": "asc"
}
},
{
"tracker_uid": "asc"
},
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"inline": "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc.start_date_time.value), ZoneId.of(\"+01:00\")).toEpochSecond(ZoneOffset.UTC)"
},
"order": "asc"
}
}
],
"from": 0,
"size": 100,
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"start_date_time": {
"gte": "2017-05-31 00:00:00",
"lte": "2017-05-31 23:59:59",
"time_zone": "+01:00"
}
}
},
{
"range": {
"journey_time_secs": {
"gte": 60
}
}
},
{
"term": {
"tracker_id": "868204007016313"
}
}
]
}
},
"must": {
"match": {
"company_id": 27283
}
}
}
},
"aggs": {
"total_idling_time": {
"sum": {
"field": "idling_time_secs"
}
},
"total_journey_time": {
"sum": {
"field": "journey_time_secs"
}
},
"total_distance": {
"sum": {
"field": "distance_km"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment