Elasticsearch Elasticsearch Nested Query

By Opster Expert Team - May 2023

Updated: Jan 28, 2024

| 2 min read

Quick links

Introduction

Elasticsearch is a widely used search and analytics engine that provides fast and flexible search capabilities. One of the advanced search features in Elasticsearch is the nested terms query, which allows you to search for documents containing specific terms within nested objects. In this article, we will dive deep into the nested terms query, its use cases, and how to implement it effectively.

Understanding Nested Objects and Nested Query

Before we discuss the nested terms query, it is essential to understand the concept of nested objects and the nested query in Elasticsearch.

What are nested objects in Elasticsearch?

Nested objects are a way to model complex relationships between entities in your documents, where a single document contains a list of inner objects with their properties.

What is a nested query in Elasticsearch?

The nested query is used to search for documents containing specific criteria within these nested objects. It works by executing a query on the nested objects and returning the parent document if the nested query matches any of the nested objects.

Implementing Nested Terms Query

To demonstrate the usage of the nested terms query, let’s consider a sample document structure representing a collection of books and their authors:

{
  "title": "Sample Book",
  "genre": "Fiction",
  "authors": [
    {
      "name": "Author 1",
      "country": "USA"
    },
    {
      "name": "Author 2",
      "country": "Canada"
    }
  ]

In this example, the “authors” field is a nested object containing information about the authors of the book. To search for books with authors from a specific country, we can use the nested terms query as follows:

1. First, ensure that the “authors” field is mapped as a nested type in your Elasticsearch index:\

PUT /books
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "genre": {
            "type": "keyword"
      }
      "authors": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "country": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

2.Next, index some sample documents:

POST /books/_doc
{
  "title": "Sample Book 1",
  "genre": "Fiction",
  "authors": [
    {
      "name": "Author 1",
      "country": "USA"
    },
    {
      "name": "Author 2",
      "country": "Israel"
    }
  ]
}

POST /books/_doc
{
  "title": "Sample Book 2",
  "genre": "Non-fiction",
  "authors": [
    {
      "name": "Author 3",
      "country": "USA"
    },
    {
      "name": "Author 4",
      "country": "Germany"
    }
  ]
}

POST /books/_doc
{
  "title": "Sample Book 3",
  "genre": "Non-fiction",
  "authors": [
    {
      "name": "Author 3",
      "country": "Brazil"
    },
    {
      "name": "Author 4",
      "country": "Brazil"
    }
  ]
}

3. Now, we can perform a nested terms query to search for books with authors from the  “USA”:

GET /books/_search
{
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "terms": {
          "authors.country": ["USA"]
        }
      }
    }
  }
}

This query will return both “Sample Book 1” and “Sample Book 2” since they both have authors from the country  “USA” genre.

Conclusion

The nested terms query is a powerful tool for searching within nested objects in Elasticsearch. By understanding and implementing this advanced query, you can unlock more complex search capabilities and improve the search experience for your users. Remember to map your nested fields correctly and use the appropriate query structure to achieve the desired search results.

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?