全部产品

search功能Demo(全)

更新时间:2021-02-18 17:01

创建Config配置头文件

Config 页面中的内容将作为后续的查询推送文档的头文件,其中包含AK,host,应用名,下拉名称 及 options 选项,等重要参数信息

<?php
//引入头文件
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
//替换对应的access key id
$accessKeyId = '<Your accessKeyId>';
//替换对应的access secret
$secret = '<Your secret>';
//替换为对应区域api访问地址,可参考应用控制台,基本信息中api地址
$endPoint = '<region endPoint>';
//替换为应用名
$appName = '<app name>';
//替换为下拉提示名称
$suggestName = '<suggest name>';
//开启调试模式
$options = array('debug' => true);
//创建OpenSearchClient客户端对象
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

搜索功能Demo(全)

<?php
header("Content-Type:text/html;charset=utf-8");
//引入头文件
require_once("Config.inc.php");
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
$searchClient = new SearchClient($client);
//创建参数对象,并指定对应参数
$params = new SearchParamsBuilder();
$params->setStart(0);
$params->setHits(20);
//设置应用名称
$params->setAppName('替换为应用名');
//设置查询query
$params->setQuery("name:'搜索'");
//设置返回格式
$params->setFormat("fulljson");
//添加排序
$params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
//设置文档过滤条件
$params->setFilter('id>0');
//添加distinct子句
$params->addDistinct(
    array('key' => 'cate_id', 'distTimes' => 1, 'distCount' => 1, 'reserved' => 'false')
);
//添加摘要
$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)
//);
//设置自定义参数
//$params->setCustomParam('a', 'b');
//$params->setCustomParam('c', 'd');
//$params->setRouteValue('1');

//设置下拉提示关联搜索请求的from_request_id
$params->setCustomParam('from_request_id','159851481919726888064081');


//添加aggregate子句
 $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)
// );
//指定粗排表达式
$params->setFirstRankName('default');
//指定精排表达式
$params->setSecondRankName('default');
//设置需返回哪些字段
$params->setFetchFields(array('id','name','phone','int_arr','literal_arr','float_arr','cate_id'));
//添加查询分析功能
$params->addQueryProcessor('替换为查询分析名称');
//执行查询并返回信息
$ret = $searchClient->execute($params->build());
//打印返回信息的内容
print_r(json_decode($ret->result,true));
//打印调试信息
echo $ret->traceInfo->tracer;