This guide shows how to implement basic search with the OpenSearch Industry Algorithm Edition PHP SDK. By the end, you will have a working PHP script that submits a search query and prints the results.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application. Find the application name and API endpoint on the Basic Information page of your application in the console.
A Resource Access Management (RAM) user with the required permissions. See Create a RAM user and Access authorization rules.
An AccessKey pair for the RAM user. See Create an AccessKey pair.
PHP with the OpenSearch PHP SDK installed.
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.
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.
Never hardcode your AccessKey pair in source code or shared files. Always load credentials from environment variables.
Step 1: Set environment variables
Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to the AccessKey ID and AccessKey secret of your RAM user.
Linux and macOS
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Replace <access_key_id> and <access_key_secret> with the values for your RAM user.
Windows
Create an environment variable file and add
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETwith their respective values.Restart Windows for the changes to take effect.
Step 2: Create a configuration file
Create Config.inc.php. This file initializes the OpenSearchClient with your credentials and endpoint, and is imported by all subsequent scripts.
<?php
// Import the OpenSearch SDK autoloader.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// Load credentials from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// Replace with your application's API endpoint.
// Find it on the Basic Information page of your application in the console.
$endPoint = '<region endPoint>';
// Replace with your application name.
$appName = '<app name>';
// Replace with the name of your drop-down suggestion model.
$suggestName = '<suggest name>';
// Enable debug mode to print trace information during development.
$options = array('debug' => true);
// Create the client.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);| Placeholder | Description |
|---|---|
<region endPoint> | API endpoint for your region, found on the application's Basic Information page in the console |
<app name> | Name of your OpenSearch application |
<suggest name> | Name of your drop-down suggestion model |
Step 3: Run a search query
Create a PHP script that imports Config.inc.php, builds a search query, and prints the results.
<?php
header("Content-Type:text/html;charset=utf-8");
// Import the configuration file created in Step 2.
require_once("Config.inc.php");
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
// Initialize the search client and parameters builder.
$searchClient = new SearchClient($client);
$params = new SearchParamsBuilder();
// Configure the query.
$params->setStart(0); // Starting offset (0-based)
$params->setHits(20); // Number of results per page
$params->setAppName('Replace with your application name'); // Target application
$params->setQuery("name:'search'"); // Search query expression
$params->setFormat("fulljson"); // Response format: full JSON
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE); // Sort by relevance score, descending
// Execute the query.
$ret = $searchClient->execute($params->build());
// Print the search results.
print_r(json_decode($ret->result, true));
// Print debug trace (available because debug mode is enabled in Config.inc.php).
echo $ret->traceInfo->tracer;Query parameters
| Method | Description |
|---|---|
setStart(0) | Starting offset for results (0-based) |
setHits(20) | Number of results to return per page |
setAppName(...) | Name of the OpenSearch application to query |
setQuery(...) | Search query expression |
setFormat("fulljson") | Response format. fulljson returns the full JSON response |
addSort('RANK', SORT_DECREASE) | Sorts results by relevance score in descending order |
Response
$ret->result contains the full JSON response. Use json_decode($ret->result, true) to parse it as a PHP array.
If debug mode is enabled in Config.inc.php, $ret->traceInfo->tracer prints the internal query trace, which helps diagnose slow queries or unexpected results.
What's next
AliyunServiceRoleForOpenSearch — Understand the service-linked role required for OpenSearch.
Access authorization rules — Configure RAM permissions for OpenSearch.