Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
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
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.
Restart Windows for the AccessKey pair to take effect.
Create the Config header file
The content of the Config file serves as the header file for the subsequent code that queries and pushes documents. It contains important parameters, such as the AccessKey pair, host, application name, drop-down suggestion name, and options.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');<?php
// Include the header file.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// User identification information.
// Read the configured AccessKey ID and AccessKey secret from environment variables.
// Before you run the sample code, you must configure the environment variables. For more information, see the "Configure environment variables" step earlier in this document.
// 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 API endpoint of the corresponding region. You can find the API endpoint in the Basic Information section of the application in the console.
$endPoint = '<region endPoint>';
// Replace with the application name.
$appName = '<app name>';
// Replace with the drop-down suggestion name.
$suggestName = '<suggest name>';
// Enable the debug mode.
$options = array('debug' => true);
// Create an OpenSearchClient object.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);Code for uploading documents
The following code uploads 10 documents to an OpenSearch application.
<?php
header("Content-Type:text/html;charset=utf-8");
// Include the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
// Specify the application table to which the data is pushed.
$tableName = 'Replace with the application table name';
// Create a client for document operations.
$documentClient = new DocumentClient($client);
// Add data. The UTF-8 byte escapes represent a sample name in Chinese.
$docs_to_upload = array();
for ($i = 0; $i < 10; $i++){
$item = array();
$item['cmd'] = 'ADD';
$item["fields"] = array(
"id" => $i + 1,
"name" => "\xE6\x90\x9C\xE7\xB4\xA2\xE8\x82\xAF\xE5\xBE\xB7\xE5\x9F\xBA".$i
);
$docs_to_upload[] = $item;
}
// Encode the data.
$json = json_encode($docs_to_upload);
// Push the documents.
$ret = $documentClient->push($json, $appName, $tableName);
// Wait 10 seconds. For the Advanced Edition, 90% of incremental updates to the primary table take effect within 10 seconds, and 99% take effect within 10 minutes. No guarantee is currently provided for secondary tables.
sleep(10);
// Create a search client object.
$searchClient = new SearchClient($client);
// Create a search parameter object.
$params = new SearchParamsBuilder();
// Set the start value of the config clause.
$params->setStart(0);
// Set the hit value of the config clause.
$params->setHits(20);
// Specify the application to search.
$params->setAppName('Replace with the application name');
// Specify the search keyword. The UTF-8 byte escapes preserve the Chinese search term.
$params->setQuery("name:'\xE6\x90\x9C\xE7\xB4\xA2'");
// Specify fulljson as the format of the returned search results.
$params->setFormat("fulljson");
// Add a sort field.
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
// Run the search and obtain the search results.
$ret = $searchClient->execute($params->build());
// Decode the JSON string.
print_r(json_decode($ret->result,true));
// Print the debugging information.
echo $ret->traceInfo->tracer;