Skip to content

Instantly share code, notes, and snippets.

@mbertrand
Last active August 28, 2019 12:03
Show Gist options
  • Save mbertrand/e73982562c5b4b8e710ad1da86c3bb91 to your computer and use it in GitHub Desktop.
Save mbertrand/e73982562c5b4b8e710ad1da86c3bb91 to your computer and use it in GitHub Desktop.
Filtered nested reverse aggregate search
{
"from": 0,
"size": 6,
"query": {
"bool": {
"should": [
{
"bool": {
"filter": {
"bool": {
"must": [
{
"term": {
"object_type": "course"
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "course_runs",
"query": {
"bool": {
"should": [
{
"term": {
"course_runs.offered_by": "OCW"
}
},
{
"range": {
"course_runs.best_end_date": {
"gte": "now"
}
}
}
]
}
}
}
}
],
"should": [
{
"nested": {
"path": "course_runs",
"query": {
"range": {
"course_runs.best_start_date": {
"to": "now"
}
}
}
}
}
]
}
}
]
}
}
}
}
]
}
},
"aggs": {
"type": {
"terms": {
"field": "object_type.keyword",
"size": 10000
}
},
"platform": {
"terms": {
"field": "platform",
"size": 10000
}
},
"topics": {
"terms": {
"field": "topics",
"size": 10000
}
},
"availability": {
"nested": {
"path": "course_runs"
},
"aggs": {
"runs": {
"date_range": {
"field": "course_runs.best_start_date",
"missing": "1901-01-01T00:00:00Z",
"keyed": false,
"ranges": [
{
"key": "availableNow",
"to": "now"
},
{
"key": "nextWeek",
"from": "now",
"to": "now+7d"
},
{
"key": "nextMonth",
"from": "now",
"to": "now+1M"
},
{
"key": "next3Months",
"from": "now",
"to": "now+3M"
},
{
"key": "next6Months",
"from": "now",
"to": "now+6M"
},
{
"key": "nextYear",
"from": "now",
"to": "now+12M"
}
]
},
"aggs": {
"courses": {
"reverse_nested": {}
}
}
}
}
},
"expired": {
"nested": {
"path": "course_runs"
},
"aggs": {
"runs": {
"filters": {
"filters": {
"expired": {
"bool": {
"must": [
{
"nested": {
"path": "course_runs",
"query": {
"bool": {
"should": [
{
"term": {
"course_runs.offered_by": "OCW"
}
},
{
"range": {
"course_runs.best_end_date": {
"gte": "now"
}
}
}
]
}
}
}
}
],
"should": [
{
"nested": {
"path": "course_runs",
"query": {
"range": {
"course_runs.best_start_date": {
"to": "now"
}
}
}
}
}
]
}
}
}
},
"aggs": {
"courses": {
"reverse_nested": {}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment