kuzzleio / kuzzle

Open-source Back-end, self-hostable & ready to use - Real-time, storage, advanced search - Web, Apps, Mobile, IoT -
https://kuzzle.io
Apache License 2.0
1.44k stars 124 forks source link

Add a document:createOrUpdate route #1895

Closed scottinet closed 3 years ago

scottinet commented 3 years ago

Document

Now this one is a bit counterintuitive. Until now, there weren't use cases for an upsert API route. Because document updates apply partial content modification to documents, it's normally not something we want to create a new document on the fly with only partial content.

However, here is a use case coming from one of our clients, which to me seems to justify the addition of that new route to Kuzzle:

Well, we have a use case where we want to maintain a list of recommendations for a user. Those reccomendations come from different sources from a kafka topic.

So when a recommendation is received we want to add it to the document for that user in our recommendations collection. If the customer is new, the document which matches the customer id will not exist yet. So we want to update the document by changing a specific set of recommendations OR create a new document with that user id and add the recommendation type. We don;t really want a collection per rec type, because that means millions of documents in each of those collections.

In that specific case, it is desirable to add partial content to a document, or to create a new one on the fly if needed.

robkane1 commented 3 years ago

Thanks for adding this feature request. Just to add to this...

Without this feature, options are:

A: Call exists, wait for a response, then based on the response, call either create or update.

B: Aggregate our messages into a ktable and fire the compiled result of that into the kuzzle collection.

Given that kuzzle supports dynamic fields on documents and elasticsearch provides upsert functionality, seems reasonable to have this available in kuzzle.

Equivalent ES:

POST test/_update/1
{
  "doc": {
    "name": "new_name"
  },
  "doc_as_upsert": true
}