Skip to content

Instantly share code, notes, and snippets.

@alexFaunt
Last active February 28, 2019 16:52
Show Gist options
  • Save alexFaunt/ecf99e647af0c8aa97d55e05dd4f896e to your computer and use it in GitHub Desktop.
Save alexFaunt/ecf99e647af0c8aa97d55e05dd4f896e to your computer and use it in GitHub Desktop.

Example mapping

{
  properties: {
    key: { type: 'keyword' },
    title: { type: 'text' },
    
    releases: {
      type: 'nested',
      properties: {
        key: { type: 'keyword' },
        title: { type: 'text' }
      }
    }
  }
}

Where the top level "title" is the Album title, and release.title is the Track title.

We are interested in querying the "Releases" array and not so interested in the top level document

i.e. if the Album title is "Texas", it has 3 releases named "Alpha", "Bravo" and "Charlie", we want to text search by the word "Bravo" and get back a result that allows us to single out the release Bravo, and ignore the other two

We figure we need to use this: https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-metrics-top-hits-aggregation.html#_top_hits_support_in_a_nested_or_reverse_nested_aggregator

A top hits aggregator inside a nested aggregator, to get back either just the releases bit, or the full document, but with a _nested property to indicate the matching potion of the document that matched

Is that the right query? What would it return? Is there a better way? What would it look like?

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