阿里云 OpenSearch 全文搜索下拉提示

class open_search_auto_downControl{
    protected $access_key = "xxxxxxx";
    protected $secret = "xxxxxxxxxxx";
    protected $app_name = "agg";
    protected $suggest_name = "agg";
    protected $host = "http://opensearch-cn-hangzhou.aliyuncs.com";
    protected $hits = 10;
    function __construct(){
        require_once(BASE_DATA_PATH.DS.'api/opensearch/CloudsearchClient.php');
        require_once(BASE_DATA_PATH.DS.'api/opensearch/CloudsearchSuggest.php');
        parent::__construct();
    }

   public function indexOp(){
   		$city_name = isset($_REQUEST['city_name']) ? trim($_REQUEST['city_name']) : "合肥市"; //城市当关键字限制输出内容
		$query = isset($_REQUEST['query']) ? trim($_REQUEST['query']) : ""; //参数
		$callback = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : "";//跨域的jsonp回调函数


		$client = new CloudsearchClient($this->access_key, $this->secret, array("host" => $this->host), "aliyun");
		$suggest = new CloudsearchSuggest($client);

		$suggest->setIndexName($this->app_name);
		$suggest->setSuggestName($this->suggest_name);
		$suggest->setHits($this->hits);
		$suggest->setQuery($query);
		$items = array();
		try {
		    $result = json_decode($suggest->search(), true);
		  if (!isset($result['errors'])) {
		    if (isset($result['suggestions']) && !empty($result['suggestions'])) {
		      $items = $result['suggestions'];
		    } else {
		      $items = array();
		    }
		  } else {
		      foreach ($result['errors'] as $error) {
		      throw new Exception($error['message'] . " request_id: " . $result['request_id'],$error['code']);
		    }
		  }
		} catch (Exception $e) {
		    // Logging the error code and error message.
		}
		//exit('{"result":["abc200121","abcr","abqp","abc"],"status":"OK","request_id":"1444632092048420500631206","AliyunPermission":"AVAILABLE"}');
		if(count($items)>0)
		{
		    foreach ($items as $key => $value) {
		    $item[$key]['label'] = $value['suggestion'];
		    }
		}
		else
		{
		    $item = array();
		}
		if (empty($callback)) {
		    echo json_encode($item);
		} else {
		    echo htmlspecialchars($callback) . "(".json_encode($item).");";
		}
   }
}

http://jqueryui.com/autocomplete/#remote-jsonp

转载于:https://my.oschina.net/sorenring/blog/519947

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值