DocumentClient
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.
DocumentClient is non-thread-safe. Do not share one instance across threads.
How it works
Create a
DocumentClientby passing anOpenSearchClientinstance to the constructor.Choose one of two upload paths:
Staged path: Call
add,update, orremovewith aMap<String,Object>of fields, then callcommitto send the staged operations.Direct path: Call
pushwith a pre-built JSON string to upload documents directly without staging.
Constructor
DocumentClient(OpenSearchClient client)Parameters
| Parameter | Type | Description |
|---|---|---|
client | OpenSearchClient | The OpenSearch client used to communicate with the service. |
Methods
add
Stages a document add operation.
void add(Map<String,Object> fields)Parameters
| Parameter | Type | Description |
|---|---|---|
fields | Map<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
| Parameter | Type | Description |
|---|---|---|
fields | Map<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
| Parameter | Type | Description |
|---|---|---|
fields | Map<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
| Parameter | Type | Description |
|---|---|---|
docsJson | String | The data to be pushed in the form of a JSON string. |
appName | String | The name of the application to which the data is to be pushed. |
tableName | String | The 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
| Parameter | Type | Description |
|---|---|---|
appName | String | The name of the application to which the data is to be committed. |
tableName | String | The 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`
| Mode | When to use |
|---|---|
push | You already have a JSON string of documents ready to send. |
commit | You 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.