HTTP 400: Bad Request

An HTTP 400 Bad Request can be caused by a variety of problems. However, it is generally a client-side issue. An HTTP 400 implies the problem is not with Elasticsearch, but rather with the request to Elasticsearch.

For example, if you have a mapping that expects a number in a particular field, and then index a document with some other data type in that field, Elasticsearch will reject it with an HTTP 400:

# Create a document with a field called "views" and the number 0
POST /myindex/mytype/1?pretty -d '{"views":0}'
{
  "_index" : "myindex",
  "_type" : "mytype",
  "_id" : "1",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

# Elasticsearch has automagically determined the "views" field to be a long (integer) data type:
GET /myindex/_mapping?pretty
{
  "myindex" : {
    "mappings" : {
      "mytype" : {
        "properties" : {
          "views" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

# Try to create a new document with a string value instead of a long in the "views" field:
POST /myindex/mytype/2?pretty -d '{"views":"zero"}'
{
  "error" : {
    "root_cause" : [ {
      "type" : "mapper_parsing_exception",
      "reason" : "failed to parse [views]"
    } ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse [views]",
    "caused_by" : {
      "type" : "number_format_exception",
      "reason" : "For input string: \"zero\""
    }
  },
  "status" : 400
}

The way to troubleshoot an HTTP 400 error is to read the response carefully and understand which part of the request is raising the exception. That will help you to identify a root cause and remediate.