All Products
Document Center

Demo code for implementing drop-down suggestions

Last Updated: Sep 09, 2021

Header file for completing basic configurations

The configurations that you specify in this header file will be used to implement the document push and document query features. The configurations include the AccessKey information, API endpoint, application name, drop-down suggestion name, and options.

// Import the header file.
use OpenSearch\Client\OpenSearchClient;

// Specify the AccessKey ID of your Alibaba Cloud account.
$accessKeyId = '<Your accessKeyId>';
// Specify the AccessKey secret of your Alibaba Cloud account.
$secret = '<Your secret>';
// Specify the endpoint of the OpenSearch API in your region. You can obtain the endpoint on the details page of the application in the OpenSearch console.
$endPoint = '<region endPoint>';
// Specify the application name.
$appName = '<app name>';
// Specify the drop-down suggestion name.
$suggestName = '<suggest name>';
// Enable the debugging mode.
$options = array('debug' => true);
// Create an OpenSearchClient object.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);


The following demo code shows how to implement drop-down suggestions. For more information about the complete demo code, see the "Complete demo code" section in the "Tutorial" topic.

// Import the header file.
use OpenSearch\Client\SuggestClient;
use OpenSearch\Util\SuggestParamsBuilder;

// Create a SuggestClient object.
$suggestClient = new SuggestClient($client);
// Create a SuggestParamsBuilder object for the drop-down suggestion to be created.
$params = SuggestParamsBuilder::build($appName, $suggestName, '<The query keyword of the drop-down suggestion to be created>', 10);

// Run the query and return drop-down suggestions.
$ret = $suggestClient->execute($params);
// Display the return results.
print_r(json_decode($ret->result, true));

// Display debugging information.
echo $ret->traceInfo->tracer;