码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-21 17:40:47      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

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).");";
		}
   }
}



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

标签:

原文地址:http://my.oschina.net/sorenring/blog/519947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!