Demo code for querying drop-down suggestions

更新时间:
复制 MD 格式

This page provides PHP sample code for querying the drop-down suggestion feature in OpenSearch Industry Algorithm Edition. The code covers three steps: configuring credentials as environment variables, creating a shared configuration header file, and running a suggestion query.

Prerequisites

Before you begin, make sure you have:

Important

Use a RAM user instead of your Alibaba Cloud root account to call API operations. Root account AccessKey pairs have full access to all resources—if leaked, the impact is severe. For instructions on creating a RAM user, see Create a RAM user.

Set environment variables

Store your credentials as environment variables instead of hardcoding them in source files.

Linux and macOS

Run the following commands. Replace <YOUR_ACCESS_KEY_ID> and <YOUR_ACCESS_KEY_SECRET> with the AccessKey ID and AccessKey secret of your RAM user.

export ALIBABA_CLOUD_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<YOUR_ACCESS_KEY_SECRET>

Windows

  1. Create an environment variable file and add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET with their respective values.

  2. Restart Windows for the changes to take effect.

Create a configuration header file

The following header file centralizes all connection settings. Both the document push and query code import this file.

Replace the placeholders with values from your OpenSearch application's details page in the console.

<?php
// Import the OpenSearch SDK autoloader.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;

// Read credentials from environment variables.
// Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET
// before running this code (see "Set environment variables" above).
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$secret      = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');

// The API endpoint for your region.
// Find this value on the application details page in the OpenSearch console.
$endPoint    = '<YOUR_REGION_ENDPOINT>';

// The name of your OpenSearch application.
$appName     = '<YOUR_APP_NAME>';

// The name of the drop-down suggestion model configured in your application.
$suggestName = '<YOUR_SUGGEST_NAME>';

// Enable debug mode to print trace information with each response.
$options = array('debug' => true);

// Create the OpenSearch client.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

Placeholders

PlaceholderDescriptionWhere to find it
<YOUR_REGION_ENDPOINT>OpenSearch API endpoint for your regionApplication details page in the OpenSearch console
<YOUR_APP_NAME>Name of your OpenSearch applicationApplication details page in the OpenSearch console
<YOUR_SUGGEST_NAME>Name of the drop-down suggestion modelApplication details page in the OpenSearch console

Query drop-down suggestions

The following code submits a suggestion query and prints the results. It returns up to 10 candidate suggestions matching the query keyword.

<?php
header("Content-Type:text/html;charset=utf-8");

// Import the shared configuration (credentials, endpoint, app name, suggest name).
require_once("Config.inc.php");
use OpenSearch\Client\SuggestClient;
use OpenSearch\Util\SuggestParamsBuilder;

// Create a SuggestClient using the shared OpenSearch client.
$suggestClient = new SuggestClient($client);

// Build the suggestion query.
// Parameters: application name, suggest model name, query keyword, max results.
$params = SuggestParamsBuilder::build($appName, $suggestName, '<YOUR_QUERY_KEYWORD>', 10);

// Run the query.
$ret = $suggestClient->execute($params);

// Print the suggestion results.
print_r(json_decode($ret->result, true));

// Print debug trace information.
echo $ret->traceInfo->tracer;

Replace <YOUR_QUERY_KEYWORD> with the keyword to query—for example, phone.

What's next

  • For more information about the complete demo code, see the "Complete sample code" section in the Tutorial topic.

  • To configure the drop-down suggestion model, see the application configuration documentation in the OpenSearch console.