All Products
Search
Document Center

OpenSearch:Demo for all search features

Last Updated:Mar 18, 2026

This topic describes how to configure the environment for a basic document search using the PHP software development kit (SDK) and provides sample code.

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 header file that contains configurations

This configuration file is used as a header for subsequent document search and push operations. It contains important parameters, such as the AccessKey, host, application name, and options. The following code provides an example:

<?php
// Import 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 code example, you must configure environment variables. For more information, see the "Configure environment variables" section.
// Replace with the corresponding AccessKey ID.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
// Replace with the corresponding AccessKey secret.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// Replace with the API endpoint of the corresponding region. You can view the endpoint on the Basic Information page of the application in the console.
$endPoint = '<region endPoint>';
// Replace with the application name.
$appName = '<app name>';
// Enable the debug mode.
$options = array('debug' => true);
// Create an OpenSearchClient client object.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

Search feature demo (Full)

The following is an example:

<?php
header("Content-Type:text/html;charset=utf-8");
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
$searchClient = new SearchClient($client);
// Create a SearchParamsBuilder object and specify the corresponding parameters.
$params = new SearchParamsBuilder();
$params->setStart(0);
$params->setHits(20);
// Set the application name.
$params->setAppName('Replace with your application name');
// Set the query.
$params->setQuery("name:'search'");
// Set the return format.
$params->setFormat("fulljson");
// Add a sort policy.
$params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
// Set the document filter condition.
$params->setFilter('id>0');
// Add a distinct clause.
$params->addDistinct(
    array('key' => 'cate_id', 'distTimes' => 1, 'distCount' => 1, 'reserved' => 'false')
);
// Add a summary.
$params->addSummary(
    array('summary_field' => 'name', 'summary_len' => 100, 'summary_ellipsis' => "...", 'summary_snippet' => 2, 'summary_element_prefix' => '', 'summary_element_postfix' => '')
);
//$params->addSummary(
//    array('summary_field' => 'name', 'summary_len' => 200)
//);
// Set custom parameters.
//$params->setCustomParam('a', 'b');
//$params->setCustomParam('c', 'd');
//$params->setRouteValue('1');

// Set the from_request_id of the associated search request.
$params->setCustomParam('from_request_id','159851481919726888064081');


// Add an aggregate clause.
 $params->addAggregate(
     array('groupKey' => 'cate_id', 'aggFun' => 'count()', 'range' => '1', 'aggSamplerThresHold' => 1, 'aggSamplerStep' => 10, 'maxGroup' => 10)
 );
// $params->addAggregate(
//     array('groupKey' => 'cate_id', 'aggFun' => 'count()', 'range' => '1', 'aggFilter' => 'id>1', 'aggSamplerThresHold' => 1, 'aggSamplerStep' => 10, 'maxGroup' => 10)
// );
// Specify the rough sort expression.
$params->setFirstRankName('default');
// Specify the fine sort expression.
$params->setSecondRankName('default');
// Set the fields to return.
$params->setFetchFields(array('id','name','phone','int_arr','literal_arr','float_arr','cate_id'));
// Add the raw_query parameter.
$params->setRawQuery("string");
// Execute the query and return the information.
$ret = $searchClient->execute($params->build());
// Print the content of the returned information.
print_r(json_decode($ret->result,true));
// Print the debug information.
echo $ret->traceInfo->tracer;