Elasticsearch Elasticsearch Inner Hit

By Opster Team

Updated: Aug 24, 2023

| 2 min read

Quick Links

Introduction 

Elasticsearch’s Inner Hits feature is a valuable tool for retrieving nested objects and parent-child documents. It allows users to return the child documents embedded within the parent document, providing a more detailed view of the data. This article will delve into the intricacies of Inner Hits, offering insights on how to effectively use this feature.

Inner Hits is particularly useful when dealing with nested objects or parent-child relationships. It allows you to retrieve not only the matching nested or child documents but also their parent documents in a single query. This can significantly improve the efficiency of your data retrieval process.

Working with nested objects: A practical example

Let’s start with an example. Suppose we have an index of books, where each book document has nested author documents. If we want to find all books written by an author named “John Doe”, we can use the Inner Hits feature to retrieve not only the matching author documents but also the parent book documents.

Here’s how you can do it:

Step 1: Defining the index

First, define your index with the nested type:

json
PUT /books
{
  "mappings": {
    "properties": {
      "authors": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

Step 2: Indexing documents

Then, index some documents:

json
PUT /books/_doc/1
{
  "title": "Book 1",
  "authors": [
    {
      "name": "John Doe"
    },
    {
      "name": "Jane Doe"
    }
  ]
}

Step 3: Utilizing the Inner Hits feature

Now, you can use the Inner Hits feature to find all books written by “John Doe”:

json
GET /books/_search
{
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "match": {
          "authors.name": "John Doe"
        }
      },
      "inner_hits": {}
    }
  }
}

In the response, you will see not only the matching author documents but also the parent book documents. This is the power of the Inner Hits feature.

Inner Hits with parent-child relationships

When dealing with parent-child relationships, the process is similar. You just need to replace the “nested” query with a “has_child” or “has_parent” query.

Performance considerations and customizations

It’s important to note that Inner Hits can be quite resource-intensive, especially when dealing with large amounts of data. Therefore, it’s recommended to use it sparingly and always monitor your Elasticsearch cluster’s performance.

In addition, you can customize the Inner Hits results by specifying the “size”, “from”, and “sort” parameters. For example, you can limit the number of Inner Hits returned or sort them by a specific field. 

json
GET /books/_search
{
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "match": {
          "authors.name": "John Doe"
        }
      },
      "inner_hits": {
        "size": 2,
        "sort": {
            "name": "asc"
        }
      }
    }
  }
}

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?