Why POST for partial update?

In REST, POST is typically used to create a resource while PUT and PATCH are used to replace the entire resource or partially update the resource. Why then, is POST used for a partial update of a resource? Even the Wikipedia artical in this commonly referenced discussion conflicts with the Elasticsearch REST API. Granted, I have only started working in Elasticsearch, but my progress is being hindered by having to relearn what HTTP methods I need to use for REST operations that only apply to one service.

One solution would be to add a server config to enable an updated REST interface in an upcoming version. Then in a later version change it so that the updated REST API is enabled by default, then remove the old API all together in the following version. That won't break backward compatibility and give developers and vendors time to update libraries and applications. I know that is a pain from a developer standpoint, being a developer myself, but I (and I assume others) would appreciate everything being standardized so we don't have to remember multiple ways of using HTTP methods for REST.

The simple answer is that Elasticsearch tries to be RESTful, but isn't dogmatic. It chooses the verbs that make the most sense for the API, not necessarily to follow RESTful conventions to a tee. Trying to be too dogmatic leads to very strange APIs.

The Update API is a bit tricky. It can provide a complete replacement of the original document, partial update, or scripted update which may or may not have an effect. The Update API is thus sometimes idempotent, and sometimes not (e.g. incrementing a counter).

Using PUT is not really appropriate because the operation may not be idempotent. An Update may also fail if the document doesn't exist, which is at odds with using PUT (where'd you expect it to create the document if it doesn't exist, since you are PUTing the resource)

Using PATCH is/was definitely an option, although it is more nuanced than most people think (good overview here: http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/, although I don't quite agree with all the points).

At the end of the day, POST was likely the simplest option at the time, without getting bogged down in the holy war of RESTful semantics :slight_smile:

We had this discussion a couple of times before. I think @polyfractal provided a nice summary, but if you are interested in other points check https://github.com/elastic/elasticsearch/pull/3213 and https://github.com/elastic/elasticsearch/issues/7030.

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