Elasticsearch Elasticsearch ids query

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction

In Elasticsearch, the IDs query is a powerful technique for retrieving documents by their unique identifier (_id). This method is particularly useful when you need to fetch specific documents from an index without performing a full-text search or filtering based on other criteria. In this article, we will explore the Elasticsearch IDs query, its advantages, and how to use it effectively.

Advantages of Elasticsearch IDs Query

  1. Performance: Since the IDs query directly targets the document’s unique identifier, it is faster than other search methods that require analyzing and scoring based on multiple fields.
  2. Precision: The IDs query ensures that only the specified documents are retrieved, eliminating the possibility of false positives or irrelevant results.
  3. Simplicity: The query is straightforward to construct and does not require complex query structures or knowledge of the underlying data schema.

Using Elasticsearch IDs Query

To use the Elasticsearch IDs query, you need to construct a query using the following format:

GET /<index>/_search
{
  "query": {
    "ids": {
      "values": ["<id1>", "<id2>", ...]
    }
  }
}

Replace <index> with the name of the index you want to search, and <id1>, <id2>, etc., with the document IDs you want to retrieve.

Example:

Let’s assume we have an index called “products” and we want to retrieve documents with the IDs “p1” and “p2”. The IDs query would look like this:

GET /products/_search
{
  "query": {
    "ids": {
      "values": ["p1", "p2"]
    }
  }
}

Elasticsearch will return the documents with the specified IDs, if they exist in the index.

Combining IDs Query with Other Queries

You can also combine the IDs query with other query types using the Elasticsearch Query DSL. For example, you can use a bool query to combine the IDs query with a range query to filter documents based on a specific date range.

Example:

Let’s assume we want to retrieve documents with the IDs “p1” and “p2” from the “products” index, but only if they were created within the last 30 days. The combined query would look like this:

GET /products/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "ids": {
            "values": ["p1", "p2"]
          }
        },
        {
          "range": {
            "created_at": {
              "gte": "now-30d"
            }
          }
        }
      ]
    }
  }
}

In this example, the bool query combines the IDs query with a range query, ensuring that only documents with the specified IDs and created within the last 30 days are returned.

Conclusion

The Elasticsearch IDs query is an efficient and precise method for retrieving documents by their unique identifier. By leveraging this query type, you can quickly access specific documents without the need for complex query structures or full-text searches. Additionally, the IDs query can be combined with other query types to further refine your search results, providing a powerful tool for working with your Elasticsearch data.