# Error: Index_not_found_exception

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:

```bash
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:

```bash
curl -X PUT "localhost:9200/my_index"
```

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

```bash
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:

```bash
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:

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

If the alias is missing, you can recreate it:

```bash
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:

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

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

```bash
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:

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