All Products
Document Center

Demo code for implementing comprehensive search features

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);

Demo code

// Import the header file.
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
$searchClient = new SearchClient($client);
// Create a parameter object and specify the parameters.
$params = new SearchParamsBuilder();
// Specify the application name.
$params->setAppName('The application name');
// Specify the query clause.
// Specify the data format of return results.
// Specify a field based on which documents are to be sorted, and a sorting method.
$params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
// Specify a filter condition.
// Add a Distinct object as a query parameter.
    array('key' => 'cate_id', 'distTimes' => 1, 'distCount' => 1, 'reserved' => 'false')
// Add a Summary object as a query parameter.
    array('summary_field' => 'name', 'summary_len' => 100, 'summary_ellipsis' => "...", 'summary_snippet' => 2, 'summary_element_prefix' => '<em>', 'summary_element_postfix' => '</em>')
//    array('summary_field' => 'name', 'summary_len' => 200)
// Add custom parameters.
//$params->setCustomParam('a', 'b');
//$params->setCustomParam('c', 'd');

// Specify the from_request_id parameter for queries that are associated with drop-down suggestions.

// Add an Aggregate object as a query parameter.
     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 a rough sort expression.
// Specify a fine sort expression.
// Specify the fields to be returned in search results.
// Add a query analyzer.
$params->addQueryProcessor('The name of the query analyzer');
// Run the query and return the results.
$ret = $searchClient->execute($params->build());
// Display the return results.
// Display debugging information.
echo $ret->traceInfo->tracer;