PUT vs POST when adding documents in elastic search

Hi,

I am new to Elasticsearch and trying to add documents in elastic index. I have got confused between PUT and POST here as both are producing same results in below scenario:

curl -H "Content-Type: application/json" -XPUT "localhost:9200/products/mobiles/1?pretty" -d"
{
"name": "iPhone 7",
"camera": "12MP",
"storage": "256GB",
"display": "4.7inch",
"battery": "1,960mAh",
"reviews": ["Incredibly happy after having used it for one week", "Best iPhone so far", "Very expensive, stick to Android"]
}
"

vs

curl -H "Content-Type: application/json" -XPOST "localhost:9200/products/mobiles/1?pretty" -d"
{
"name": "iPhone 7",
"camera": "12MP",
"storage": "256GB",
"display": "4.7inch",
"battery": "1,960mAh",
"reviews": ["Incredibly happy after having used it for one week", "Best iPhone so far", "Very expensive, stick to Android"]
}
"

Welcome.

You do PUT when you want to set the _id:

PUT index/_doc/1
{ ... }

You do POST when you don't want to set the _id and let elasticsearch generate it:

POST index/_doc
{ ... }

Thank You David!!
But I have checked that POST is also allowed with explicit id, why this is not restricted in elastic

I don't know.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.