Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matthiasg/2b8748cfdf919ce77c3fd0438613cee6 to your computer and use it in GitHub Desktop.
Save matthiasg/2b8748cfdf919ce77c3fd0438613cee6 to your computer and use it in GitHub Desktop.
sample elasticsearch query to sort parent based on child field
{
"query":{
"has_child":{
"query":{
"function_score":{
"functions":[
{
"field_value_factor":{
"factor":1,
"field":"count"
}
}
]
}
},
"score_mode":"avg",
"type":"my_child_doc_type"
}
}
}
@MosesMansaray
Copy link

Brilliant, Thanks. This worked like a charm for a POC I was working on that needed parents sorted based by children field... I also needed to:

  1. query against the parent document.
  2. return only the matched results that has children.
  3. sorted by child.field.value.

This has fall short on 1. With any sort of query resulting to "nested: QueryParsingException...No query registered for [functions]]".

Any Idea on how to expand this to take say a "query_string"?

Cheers.

@mrkk
Copy link

mrkk commented Jan 14, 2019

what if we needed to have results where filter needs to apply to both parent and child ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment