DocumentClient

Updated at:

DocumentClient uploads documents to an OpenSearch application. Use add, update, or remove to stage document operations, then call commit to send them to the index. Alternatively, call push directly with a pre-built JSON string to upload documents without staging.

Important

DocumentClient is non-thread-safe. Do not share one instance across threads.

How it works

  1. Create a DocumentClient by passing an OpenSearchClient instance to the constructor.

  2. Choose one of two upload paths:

    • Staged path: Call add, update, or remove with a Map<String,Object> of fields, then call commit to send the staged operations.

    • Direct path: Call push with a pre-built JSON string to upload documents directly without staging.

Constructor

DocumentClient(OpenSearchClient client)

Parameters

ParameterTypeDescription
clientOpenSearchClientThe OpenSearch client used to communicate with the service.

Methods

add

Stages a document add operation.

void add(Map<String,Object> fields)

Parameters

ParameterTypeDescription
fieldsMap<String,Object>The fields of the document to add.

Exceptions

Exception
JSONException

update

Stages a document update operation.

void update(Map<String,Object> fields)

Parameters

ParameterTypeDescription
fieldsMap<String,Object>The fields of the document to update.

Exceptions

Exception
JSONException

remove

Stages a document remove operation.

void remove(Map<String,Object> fields)

Parameters

ParameterTypeDescription
fieldsMap<String,Object>The fields identifying the document to remove.

Exceptions

Exception
JSONException

push

Uploads data in push mode.

OpenSearchResult push(String docsJson, String appName, String tableName)

Parameters

ParameterTypeDescription
docsJsonStringThe data to be pushed in the form of a JSON string.
appNameStringThe name of the application to which the data is to be pushed.
tableNameStringThe table to which the data is to be pushed.

Returns

OpenSearchResult — the result of the push operation.

Exceptions

Exception
OpenSearchException
OpenSearchClientException

commit

Uploads data in commit mode.

OpenSearchResult commit(String appName, String tableName)

Parameters

ParameterTypeDescription
appNameStringThe name of the application to which the data is to be committed.
tableNameStringThe table to which the data is to be committed.

Returns

OpenSearchResult — the result of the commit operation.

Exceptions

Exception
OpenSearchException
OpenSearchClientException

Usage notes

`push` vs `commit`

ModeWhen to use
pushYou already have a JSON string of documents ready to send.
commitYou staged operations with add, update, or remove and want to send them in a single call.

Thread safety

DocumentClient is non-thread-safe. Each thread that uploads documents must use its own DocumentClient instance.