All Products
Search
Document Center

OpenSearch:Push demo - push data

Last Updated:Jun 22, 2026

Learn how to configure and use the PHP SDK to push documents to OpenSearch, with sample code for adding, updating, and deleting data.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Create a Config header file

The Config file is a header file for document query and push operations. It contains parameters such as your AccessKey pair, host, and application name.

<?php
// Import the header file.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;

// User identity information.
// Read the AccessKey ID and AccessKey secret from environment variables.
// Before you run the sample code, you must configure environment variables. For more information, see the "Set environment variables" section in this topic.
// Replace with your AccessKey ID.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
// Replace with your AccessKey secret.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// Replace with the endpoint of your region. You can find the endpoint on the Basic Information page of your application in the console.
$endPoint = '<region_endpoint>';
// Replace with your application name.
$appName = '<app_name>';
// Enable the debug mode.
$options = array('debug' => true);
// Create an OpenSearchClient object.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

Push

Upload document code

Add data

The following sample code uploads 10 documents to an OpenSearch application.

<?php
// Reference the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table in the application to which you want to push data.
$tableName = 'your_table_name';
// Create a DocumentClient object for document operations.
$documentClient = new DocumentClient($client);
// Add data.
$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 documents in the JSON format.
$json = json_encode($docs_to_upload);
// Submit the documents.
$ret = $documentClient->push($json, $appName, $tableName);

Update data

<?php
// Reference the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table in the application to which you want to push data.
$tableName = 'your_table_name';
// Create a DocumentClient object for document operations.
$documentClient = new DocumentClient($client);
// Prepare the data.
$docs_to_upload = array();
$item = array();
// The UPDATE command is supported only in OpenSearch Pro.
$item['cmd'] = 'UPDATE';
$item["fields"] = array(
    "id" => 1,
    "name" => "OpenSearch"
);
$docs_to_upload[] = $item;
// Encode the documents in the JSON format.
$json = json_encode($docs_to_upload);
// Submit the documents.
$ret = $documentClient->push($json, $appName, $tableName);

Delete data

<?php
// Reference the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table in the application to which you want to push data.
$tableName = 'your_table_name';
// Create a DocumentClient object for document operations.
$documentClient = new DocumentClient($client);
// Prepare the data.
$docs_to_upload = array();
$item = array();
$item['cmd'] = 'DELETE';
$item["fields"] = array(
    "id" => 1,   // Specify the primary key ID of the data that you want to delete.
);
$docs_to_upload[] = $item;
// Encode the documents in the JSON format.
$json = json_encode($docs_to_upload);
// Submit the documents.
$ret = $documentClient->push($json, $appName, $tableName);

Demo for pushing and searching data

Upload document code

The following example uploads 10 documents to an OpenSearch application and then searches the data.

<?php
header("Content-Type:text/html;charset=utf-8");
// Reference the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;

// Specify the table in the application to which you want to push data.
$tableName = 'your_table_name';
// Create a DocumentClient object for document operations.
$documentClient = new DocumentClient($client);
// Add data.
$docs_to_upload = array();
for ($i = 0; $i < 10; $i++){
    $item = array();
    $item['cmd'] = 'ADD';
    $item["fields"] = array(
        "id" => $i + 1,
        "name" => "search_kfc".$i
        );
    $docs_to_upload[] = $item;
}
// Encode the data.
$json = json_encode($docs_to_upload);
// Submit the documents.
$ret = $documentClient->push($json, $appName, $tableName);

// Wait for 10 seconds. For OpenSearch Pro, 90% of incremental data in the primary table takes effect within 10 seconds, and 99% takes effect within 10 minutes. The latency for attached tables is not guaranteed.
sleep(10);

// Instantiate a search class.
$searchClient = new SearchClient($client);
// Instantiate a search parameter class.
$params = new SearchParamsBuilder();
// Set the start value for the config clause.
$params->setStart(0);
// Set the hit value for the config clause.
$params->setHits(20);
// Specify an application for the search.
$params->setAppName('your_app_name');
// Specify the search keyword.
$params->setQuery("name:'search'");
// Set the format of the search result to JSON.
$params->setFormat("fulljson");
// Add a sort field.
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
// Execute the search and obtain the result.
$ret = $searchClient->execute($params->build());
// Decode the JSON string.
print_r(json_decode($ret->result,true));
// Print the debug information.
echo $ret->traceInfo->tracer;