I'm just starting to use ElasticSearch. And I tried to know how to insert documents. I only found examples using the PUT method : $ curl -XPUT 'http://localhost:9200/...'
But it also seems to work using POST. Is there any difference between these two methods?
Thank you.
PUT x (if x identifies a resource ): "Replace the content of the resource identified by x with my content." PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."
It essentially means that POST request-URI should be of a collection URI. PUT method is idempotent. So if we retry a request multiple times, that should be equivalent to a single request invocation.
UUID's are superior in every way except storage space. Ints for IDs are a holdover from when DB storage was a larger concern that it is today. The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity.
PUT works as specific while POST work as abstract. If you send the same PUT request multiple times, the result will remain the same but if you send the same POST request multiple times, you will receive different results. PUT method is idempotent whereas POST method is not idempotent.
Generally when using a REST API:
- POST is used to create a resource, where the server will pick an ID.
- PUT is used to update OR PLACE a resource at a known ID.
Doc creation examples in the ES documentation show the caller picking an ID.
Like so:
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
Since the caller is picking the ID a PUT seems appropriate here.
BUT
using POST Elasticsearch can also generate an ID for you.
$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With