Error: Index_not_found_exception

Better Stack Team
Updated on October 26, 2024

The index_not_found_exception error in Elasticsearch occurs when a request is made to an index that does not exist. This error typically happens when you're trying to query, delete, or index documents into an Elasticsearch index that hasn’t been created yet or was accidentally deleted.

Here’s how you can troubleshoot and resolve this error.

Steps to Resolve index_not_found_exception

1. Verify the Existence of the Index

First, ensure the index you’re trying to interact with actually exists.

You can list all indices in your Elasticsearch cluster by running the following curl command:

 
curl -X GET "localhost:9200/_cat/indices?v"

This will return a list of all existing indices in your Elasticsearch cluster. Check if the index you are referencing is present.

2. Create the Index (if it doesn't exist)

If the index doesn't exist, you will need to create it before trying to index or query documents.

Create an index using the following command:

 
curl -X PUT "localhost:9200/my_index"

You can also define custom mappings or settings when creating the index:

 
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}
'

Replace my_index with the actual name of the index you want to create.

3. Check Index Name in Query/Request

Sometimes the index_not_found_exception can occur due to a typo or misnamed index in your query. Double-check the index name in your query or request to ensure that it matches the exact name of the index in Elasticsearch.

For example, if your index name is logs-2024-09, ensure that your query is targeting that specific index:

 
curl -X GET "localhost:9200/logs-2024-09/_search"

Elasticsearch is case-sensitive, so ensure that there are no case mismatches in the index name.

4. Verify Index Aliases

If you’re using an alias to refer to an index and you get the index_not_found_exception, it’s possible that the alias is not correctly pointing to the index. You can check if the alias exists and what it points to with:

 
curl -X GET "localhost:9200/_cat/aliases?v"

If the alias is missing, you can recreate it:

 
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
  "actions": [
    { "add": { "index": "my_index", "alias": "my_alias" } }
  ]
}
'

5. Ensure Index is Not Closed

If an index exists but is closed, Elasticsearch will throw an index_not_found_exception. You can check the status of the index by running:

 
curl -X GET "localhost:9200/_cat/indices?v"

If the index is listed as closed, you can reopen it using:

 
curl -X POST "localhost:9200/my_index/_open"

Replace my_index with the name of your index.

6. Check Elasticsearch Logs

If the error persists, check the Elasticsearch logs for any additional details or errors that might help you identify the problem. The logs can usually be found in:

  • /var/log/elasticsearch/elasticsearch.log
  • /usr/share/elasticsearch/logs/

7. Check the Index Lifecycle Management (ILM) Policy

If you have an ILM policy that automatically deletes old indices or changes their states, an index may have been deleted or closed as part of the policy. You can verify ILM policies and their actions with:

 
curl -X GET "localhost:9200/_ilm/policy"

If necessary, adjust the ILM policies to prevent unexpected index closures or deletions.

Summary of Fixes:

  • Verify the index exists by listing all indices.
  • Create the index if it doesn’t exist.
  • Double-check the index name in your queries.
  • Verify alias configurations, if you're using aliases.
  • Open the index if it is closed.
  • Check the Elasticsearch logs for more details.
  • Review ILM policies to prevent automatic index deletion.

By following these steps, you should be able to resolve the index_not_found_exception error.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github