Use the OpenSearch PHP SDK to retrieve top searches or hints for your application. Both features use the same OpenSearchClient — the only difference is the URI path (/actions/hot for top searches, /actions/hint for hints).
Prerequisites
Before you begin, make sure you have:
An OpenSearch Industry Algorithm Edition application
A RAM user with the required permissions (see Access authorization rules)
An AccessKey pair for the RAM user (see Create an AccessKey pair)
Use a Resource Access Management (RAM) user instead of your Alibaba Cloud root account. The root account AccessKey pair grants unrestricted access to all API operations. Grant only the permissions required for OpenSearch using the AliyunServiceRoleForOpenSearch role. Do not embed the AccessKey pair in source code or other locations where it might be exposed.
Set environment variables
Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET in your environment. The sample code reads credentials from these variables at runtime.
Linux and macOS — Run the following commands, replacing the placeholders with your RAM user's AccessKey ID and AccessKey secret:
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
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETwith their respective values, then restart Windows for the changes to take effect.
Sample code
The following examples use OpenSearchClient to call the top searches and hints endpoints. Replace these placeholders before running:
| Placeholder | Description |
|---|---|
<endpoint> | OpenSearch API endpoint for your region |
<app-name> | Name of your OpenSearch application |
<model-name> | Name of the top search or hint model. If your application has only one model of this type, leave this blank. |
Top searches
Calls /apps/{appName}/actions/hot to return the most frequently queried terms.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// Read credentials from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$endPoint = '<endpoint>';
$appName = '<app-name>';
$modelName = '<model-name>'; // Optional.
$options = array('debug' => true);
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);
$uri = "/apps/{$appName}/actions/hot";
$params = [];
$params['hit'] = 10;
$params['sort_type'] = 'default';
$params['user_id'] = '1231453';
$params['model_name'] = $modelName; // Optional. Specify the model name of the top searches or hints.
try {
$ret = $client->get($uri, $params);
print_r(json_decode($ret->result, true));
} catch (\Throwable $e) {
print_r($e);
}Hints
Calls /apps/{appName}/actions/hint to return suggested completions as users type.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// Read credentials from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$endPoint = '<endpoint>';
$appName = '<app-name>';
$modelName = '<model-name>'; // Optional.
$options = array('debug' => true);
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);
$uri = "/apps/{$appName}/actions/hint";
$params = [];
$params['hit'] = 10;
$params['sort_type'] = 'default';
$params['user_id'] = '1231453';
$params['model_name'] = $modelName; // Optional. Specify the model name of the top searches or hints.
try {
$ret = $client->get($uri, $params);
print_r(json_decode($ret->result, true));
} catch (\Throwable $e) {
print_r($e);
}