Elasticsearch Elasticsearch Nest Terms Query

By Opster Team

Updated: Jul 20, 2023

| 2 min read

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. 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.

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",
  "authors": [
    {
      "name": "Author 1",
      "genre": "Fiction"
    },
    {
      "name": "Author 2",
      "genre": "Non-fiction"
    }
  ]
}

In this example, the “authors” field is a nested object containing information about the authors of the book. To search for books with authors in a specific genre, 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"
      },
      "authors": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "genre": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

2. Index some sample documents:

POST /books/_doc
{
  "title": "Sample Book 1",
  "authors": [
    {
      "name": "Author 1",
      "genre": "Fiction"
    },
    {
      "name": "Author 2",
      "genre": "Non-fiction"
    }
  ]
}

POST /books/_doc
{
  "title": "Sample Book 2",
  "authors": [
    {
      "name": "Author 3",
      "genre": "Fiction"
    },
    {
      "name": "Author 4",
      "genre": "Fiction"
    }
  ]
}

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

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

This query will return both “Sample Book 1” and “Sample Book 2” since they both have authors in the “Fiction” 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?