ElasticSearch fix 406 Not Acceptable: error
I'm trying to index a document with ElasticSearch PHP client (version 8.8), and I'm getting an error when trying to execute the create document request:
406 Not Acceptable: {"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported","status":406}
The weird thing is that the client allowed me to create an index, but when I tried to index a document, I got an error...😕
My configuration is:
- ElasticSearch PHP client: version 8.8
- ElasticSearch server: version 7.10
The issue
To better understand the issue, I'll use curl to reproduce it.
Here is what the ElasticSearch PHP clients send to the server:
curl --location 'http://localhost:9200/products_1/_doc/1' \
--header 'Content-Type: application/vnd.elasticsearch+json; compatible-with=8' \
--data '{
"title": "hello"
}'
Notice anything weird? Yes, the Content-Type
header is a bit unusual.
Content-Type: application/vnd.elasticsearch+json; compatible-with=8
This tells the ElasticSearch server: I expect a response of a type application/vnd.elasticsearch+json
and compatible-with=8
which is ElasticSearch version 8.
The ElasticSearch server responds:
Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported
which means I cannot return this content type because I'm not supporting it.
If I change the Content-Type to application/json
the request is accepted
curl --location 'http://localhost:9200/products_1/_doc/1' \
--header 'Content-Type: application/json' \
--data '{
"title": "hello"
}'
responds
{
"_index": "products_1",
"_type": "_doc",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
How to fix this?
As a general rule of thumb, the client's major version should match the server's major version.
There are three options:
- Update the client to match the server version
- Update the server version to match the client
- Configure your client and change the request Content-Type.
In my case, option 3 is out of the question; the PHP ElasticSearch client doesn't support custom Content-Type, or I cannot find how to do it.
Note that a Python client has that configuration, so check your client supports custom Content-Type.