All Products
Search
Document Center

DocumentClient

Last Updated: Sep 09, 2021

Overview

The DocumentClient class is used to manage documents in an application. For example, you can upload, delete, or update documents. You can also upload multiple documents at a time.

$docs

Attribute definition

$docs

Parameter description

Parameter

Type

Description

$docs

array

The collection of documents to be uploaded.


Constructor

Method definition

void  OpenSearch\Client\DocumentClient::__construct($openSearchClient)

Parameter description

Parameter

Type

Description

$openSearchClient

\OpenSearch\Client\OpenSearchClient

The OpenSearchClient object. The OpenSearchClient class is a basic class that can be used to calculate signatures, interact with servers, and return the results.


add

Note

This method adds a document only to the buffer of the SDK client. To submit the document to the server, call the commit method. You can call the add method multiple times to add multiple documents to the buffer and then call the commit method to submit the documents to the server at a time.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::add( $fields)

Parameter description

Parameter

Type

Description

$fields

array

All fields of the document to be added. Example: array("id" => 1, "name" => "tony").


update

Note

This method adds a document only to the buffer of the SDK client. To submit the document to the server, call the commit method. You can call the update method multiple times to add multiple documents to the buffer and then call the commit method to submit the documents to the server at a time. Standard applications do not support the update method or partial updates of documents. You can call the add method to add documents or perform full updates of documents.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::update( $fields)

Parameter description

Parameter

Type

Description

$fields

array

All fields of the document to be updated. Example: array("id" => 1, "name" => "tony").


remove

Note

This method adds a document only to the buffer of the SDK client. To submit the document to the server, call the commit method. You can call the remove method multiple times to add multiple documents to the buffer and then call the commit method to submit the documents to the server at a time.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::remove( $fields)

Parameter description

Parameter

Type

Description

$fields

array

The primary key field of the document to be deleted. Example: array("id" => 1). To delete a document, you need only to specify the primary key field of the document.


push

Uploads multiple documents to the buffer of the SDK client. The documents are also submitted to the server.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::push( $docsJson,  $appName,  $tableName)

Parameter description

Parameter

Type

Description

$docsJson

string

The JSON string that contains a list of documents to be uploaded. Example: [{"cmd":"ADD","fields":{"id":"1","name":"tony" }},...]

$appName

string

The name or ID of the application to which you want to upload the documents.

$tableName

string

The name of the table to which you want to upload the documents.


commit

Note

Submits documents in the buffer of the SDK client to the server. Before the documents are submitted to the server, the documents in the buffer are cleared. If the server returns an error response and requires a retry, you need to upload the documents to the buffer again before you call the commit method. This prevents data loss.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::commit( $appName,  $tableName)

Parameter description

Parameter

Type

Description

$appName

string

The name or ID of the application to which you want to upload the documents.

$tableName

string

The name of the table to which you want to upload the documents.


pushOneDoc

Uploads one document to the buffer of the SDK client.

Method definition

\OpenSearch\Generated\Common\OpenSearchResult     OpenSearch\Client\DocumentClient::pushOneDoc( $fields,  $cmd)

Parameter description

Parameter

Type

Description

$fields

array

All fields of the document to be uploaded. Example: array("id" => 1, "name" => "tony").

$cmd

string

The type of operation that you want to perform on the document. For advanced applications, valid values are ADD, UPDATE, and DELETE. For standard applications, valid values are ADD and DELETE.


Demo code

You can use the following demo code to implement the document push feature. Replace the configurations in the demo code with your own information. For more information about the header files such as the Config.inc.php header file, see the "Tutorial" topic.

<?php
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table to which data is to be uploaded.
$tableName = 'The table to which data is to be uploaded';
// Create a DocumentClient object.
$documentClient = new DocumentClient($client);

// Add a document.
//$json = json_encode(array(array("cmd" => "add", "fields" => array("id" => 1, "name" => "php opensearch"))));

// Use a loop to add multiple test documents.
$docs_to_upload = array();
for ($i = 0; $i < 10; $i++){
    $item = array();
    $item['cmd'] = 'ADD';
    $item["fields"] = array(
        "id" => $i + 1,
        "name" => "Search".$i
        );
    $docs_to_upload[] = $item;
}

// Encode the data to be uploaded into a JSON string.
$json = json_encode($docs_to_upload);

// Upload the documents to the specified application.
$ret = $documentClient->push($json, $appName, $tableName);
// Display the return results.
print_r(json_decode($ret->result, true));
// Display debugging information.
echo $ret->traceInfo->tracer;

Debugging

The preceding configurations are sufficient to meet basic search requirements. However, to optimize search performance and improve the relevance of search results requires a long process of trial and error and iterations. In this process, you may encounter issues or search results may not meet expectations. In these cases, you can call the getRequestUrl method in the following sample code to obtain detailed information about the requests in question, and then use the information for troubleshooting. When you encounter an issue, you can seek help from the TradeManager or DingTalk support group of OpenSearch. You must provide debugging information so that the support engineers can analyze and identify the cause based on the information.

echo $ret->traceInfo->tracer;