update_document

The function update_document(body, collection_name, partial_update, doc_as_upsert uploads a single document to a collection.

Input

  • body (document) – Represents the document to upload. The body must include the ID of the document to be updated, and the structure must conform to the collection schema configuration file. It must be of type dictionary.

  • collection_name (str) – Specifies the name of the Collection into which to load the document.

  • partial_update (bool) - Specifies if to perform partial or full update. For partial_update = True, only the fields included in body will be updated. If partial_update = False, the collection document will be replaced. Default value is False.

  • doc_as_upsert (bool) - Specifies the behavior if the document does not exit in the collection. if doc_as_upsert = True, the document will be added as is to the collection. If doc_as_upsert = False, no action will be taken. Default value is False;

Example

new_document= {"id": "131", "name": "dave","age": 43}

hyperspace_client.update_document(body=new_document, 
                                  collection_name='partial_update', 
                                  partial_update=True,
                                  doc_as_upsert=False);

Response

The following response should be received –

{'status': 'OK', 'code': 200, 'message': 'Document was successfully updated'}

At the moment, updating a field of type "dense_vector" is not possible. When updating a document, the "body" document must not include fields of type "dense_vector". If the corresponding database document (meaning the document with the same ID in the database) includes such a field, it will remain unchanged.

Conditional update

Hyperspace supports conditional update. In this case, body is provided as JS script that includes the relevant conditions.

Example -

body = {
  "id": 533,
  "script": {
      "lang": "js",
      "source": """
        if (ctx._source.tag != params.tag || ctx._source.tagType != params.tagType) {
            ctx._source.tag = params.tag;
            ctx._source.tagType = params.tagType;
        } else {
            ctx.op = "noop";
        }
      """,
      "params": {
          "tagType": "some tag type",
          "tag": "some tag",
      }
  }
}

client.update_document(body=body, collection_name=collection, partial_update=True)

In the above example, the document with id "533" will be updated as follows -

  • if params["tag"] == document["tag"] and params["tagType "] == document["tagType "], no update will be performed

  • Otherwise, document will pe updated as usual.

Last updated