Unknown key for create index – How to solve this Elasticsearch error

Unknown key for create index – How to solve this Elasticsearch error

Opster Team

February-21, Version: 1.7-8.0

To understand why this error arises when an index doesn’t have a defined key in the request body, you should run the Elasticsearch Error Check-Up . It will help you resolve the issue and prevent it from occuring in the future.

This guide will help you check for common problems that cause the log “unknown key for create index” to appear. It’s important to understand the issues related to the log, so to get started, read the general overview on common issues and tips related to the Elasticsearch concepts: admin, indices and index.

Background

Each index created can have specific settings, mapping definitions and index aliases. The above error arises when the request body of the create index API is not correctly formed. 

Refer to this guide to learn more about various parameters and settings included under each section of the request body. For example – If the index setting (like analysis) is not defined under the request body’s settings section, then the below error will arise.

How to reproduce this exception

Create an index, with the below settings and mappings.

Index mapping:

PUT /my-index
{
 "settings": {
   "number_of_shards": 2,
   "number_of_replicas": 1
 },
 "analysis": {
   "analyzer": {
     "my_analyzer": {
       "type": "custom",
       "tokenizer": "standard",
       "filter": [
         "lowercase"
       ]
     }
   }
 },
 "mappings": {
   "properties": {
     "question": {
       "type": "text",
       "analyzer": "my_analyzer"
     }
   }
 }
}

The response will be:

{
 "error": {
   "root_cause": [
     {
       "type": "parse_exception",
       "reason": "unknown key [analysis] for create index"
     }
   ],
   "type": "parse_exception",
   "reason": "unknown key [analysis] for create index"
 },
 "status": 400
}

How to fix this exception

The analysis section needs to be located inside the top-level settings section.

Modify the index mapping:

PUT /my-index
{
 "settings": {
   "number_of_shards": 2,
   "number_of_replicas": 1,
   "analysis": {                      // note this
     "analyzer": {
       "my_analyzer": {
         "type": "custom",
         "tokenizer": "standard",
         "filter": [
           "lowercase"
         ]
       }
     }
   }
 },
 "mappings": {
   "properties": {
     "question": {
       "type": "text",
       "analyzer": "my_analyzer"
     }
   }
 }
}

Log Context

Log”unknown key [{}] for create index”classname  is CreateIndexRequest.java We extracted the following from Elasticsearch source code for those seeking an in-depth context :

mapping(entry1.getKey(); (Map) entry1.getValue());
  }
  } else if (ALIASES.match(name; deprecationHandler)) {
  aliases((Map) entry.getValue());
  } else {
  throw new ElasticsearchParseException("unknown key [{}] for create index"; name);
  }
  }
  return this;
  }

 

Run the Check-Up to get a customized report like this:

Analyze your cluster