All Products
Search
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.

<?php
// Import the header file.
require_once("../OpenSearch/Autoloader/Autoloader.php");
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

<?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 parameter object and specify the parameters.
$params = new SearchParamsBuilder();
$params->setStart(0);
$params->setHits(20);
// Specify the application name.
$params->setAppName('The application name');
// Specify the query clause.
$params->setQuery("name:'Search'");
// Specify the data format of return results.
$params->setFormat("fulljson");
// 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.
$params->setFilter('id>0');
// Add a Distinct object as a query parameter.
$params->addDistinct(
    array('key' => 'cate_id', 'distTimes' => 1, 'distCount' => 1, 'reserved' => 'false')
);
// Add a Summary object as a query parameter.
$params->addSummary(
    array('summary_field' => 'name', 'summary_len' => 100, 'summary_ellipsis' => "...", 'summary_snippet' => 2, 'summary_element_prefix' => '<em>', 'summary_element_postfix' => '</em>')
);
//$params->addSummary(
//    array('summary_field' => 'name', 'summary_len' => 200)
//);
// Add custom parameters.
//$params->setCustomParam('a', 'b');
//$params->setCustomParam('c', 'd');
//$params->setRouteValue('1');

// Specify the from_request_id parameter for queries that are associated with drop-down suggestions.
$params->setCustomParam('from_request_id','159851481919726888064081');


// Add an Aggregate object as a query parameter.
 $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 a rough sort expression.
$params->setFirstRankName('default');
// Specify a fine sort expression.
$params->setSecondRankName('default');
// Specify the fields to be returned in search results.
$params->setFetchFields(array('id','name','phone','int_arr','literal_arr','float_arr','cate_id'));
// 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.
print_r(json_decode($ret->result,true));
// Display debugging information.
echo $ret->traceInfo->tracer;