Hyperspace Docs
Hyperspace Homepage
  • Getting started
    • Overview
      • Hyperspace Advantages
      • Hyperspace Search
    • Quick Start
  • flows
    • Setting Up
      • Installing the Hyperspace API Client
      • Connecting to the Hyperspace Server
      • Creating a Database Schema Configuration File
        • Vector Similarity Metrics
        • Index Type Methods
      • Creating a Collection
      • Uploading Data to a Collection
      • Building and Running Queries
        • Building a Lexical Search Query
        • Building a Vector Search Query
        • Building a Hybrid Search Query
      • Retrieving Results
    • Data Collections
      • Uploading Data
      • Accessing Data
      • Supported Data Types
    • Queries
      • DSL Query interface
        • Aggregations
        • Bool Query
        • Candidate Generation and Metadata Filtering
        • Scoring and Ranking
  • Reference
    • Hyperspace Query Flow
    • Features and Benefits
    • Search Processing Unit (SPU)
    • Hyperspace Document Prototype
  • API Documentation
    • Hyperspace Client
      • add_batch
      • add_document
      • async_req
      • clear_collection
      • collections_info
      • commit
      • create_collection
      • delete_collection
      • delete_by_query
      • dsl_search
      • get_schema
      • get_document
      • reset_password
      • search
      • update_by_query
      • update_document
    • DSL Query Framework
      • Aggregations
        • Cardinality Aggregation
        • Date Histogram
        • Metric Aggregations
        • Terms Aggregation
      • Bool Queries
        • Free Text Search
        • 'match' Clause
        • 'filter' Clause
        • 'must' Clause
        • 'must_not' Clause
        • 'should' Clause
        • 'should_not' Clause
      • Candidate Generation and Metadata Filtering
        • Geo Coordinates Match
        • Range Match
        • Term Match
      • Scoring and Ranking
        • Boost
        • 'dis_max'
        • Function Score
        • Rarity Score (TF-IDF)
  • Releases
    • 2024 Releases
Powered by GitBook
On this page
  1. API Documentation
  2. Hyperspace Client

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) - "Update or Insert" operation. When upsert = True, the upload command will attempt to run your update script. If the document exists, it will be updated. Otherwise, it will be uploaded as a new document.

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);
Document doc = new Document();
doc.setId("131");
doc.putAdditionalProperty("name", "dave");
doc.putAdditionalProperty("age", 43);

boolean partialUpdate = true;
boolean docAsUpsert = true;                        
hyperspaceClient.updateDocument(collection_name, new_document, partialUpdate, docAsUpsert);
const new_document= {"id": "131",
                        "name": "dave",
                         "age": 43};
const partialUpdate = true;
const docAsUpsert = true;

await hyperspaceClient.update(collectionName, new_document, partialUpdate, docAsUpsert);

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 -

// Not yet supported
JsonObject script = new JsonObject();

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",
          }
      }
      
Document doc = new Document();      
doc.setId("533");
doc.putAdditionalProperty("script", script);
client.updateDocument(collectionName, doc, true, false);
const 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",
          }
      }
    };
    
await hyperspaceClient.update(collection_name=collection, body=body, partial_update=True);

Response

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 be updated as usual.

Previousupdate_by_queryNextDSL Query Framework

Last updated 11 months ago