ElasticSearch全文搜索引擎 ,是一款很好的搜索框架!用来做网站的搜索,可以对数据库压力起到一定的缓解作用!
之前给大家带来的是用curl来实现的大家有兴趣可以看看
http://www.thinkphp.cn/topic/29297.html
今天这里给大家带来的是官网提供的 PHP API
整合thinkphp3.2
下载文件解压到
ThinkPHP\Library\Vendor\<?php <br />
namespace Home\Controller;<br>
<br>
use Think\Controller;<br>
<br>
class EcController extends Controller<br>
{<br>
<br>
public function _initialize()<br>
{<br>
Vendor('Elasticsearch.autoload');<br>
$params['hosts'] = array(<br>
'127.0.0.1:9200'<br>
);<br>
$this->client = new \Elasticsearch\Client($params);<br>
}<br>
<br>
public function create_index()<br>
{<br>
$indexParams['index'] = 'my_index';<br>
$indexParams['type'] = 'my_index';<br>
$indexParams['body']['settings']['number_of_shards'] = 2;<br>
$indexParams['body']['settings']['number_of_replicas'] = 0;<br>
$this->client->create($indexParams);<br>
}<br>
<br>
public function add_document()<br>
{<br>
$params = array();<br>
$params['body'] = array(<br>
'testField' => 'dfdsfdsf'<br>
);<br>
$params['index'] = 'my_index';<br>
$params['type'] = 'my_index';<br>
$params['id'] = 'w1231313';<br>
$ret = $this->client->index($params);<br>
}<br>
<br>
public function delete_index()<br>
{<br>
$deleteParams['index'] = 'my_index';<br>
$this->client->indices()->delete($deleteParams);<br>
}<br>
<br>
public function delete_document()<br>
{<br>
$deleteParams = array();<br>
$deleteParams['index'] = 'my_index';<br>
$deleteParams['type'] = 'my_index';<br>
$deleteParams['id'] = 'AU4Kmmj-WOmOrmyOj2qf';<br>
$retDelete = $this->client->delete($deleteParams);<br>
}<br>
public function update_document()<br>
{<br>
$updateParams = array();<br>
$updateParams['index'] = 'my_index';<br>
$updateParams['type'] = 'my_index';<br>
$updateParams['id'] = 'my_id';<br>
$updateParams['body']['doc']['asas'] = '111111';<br>
$response = $this->client->update($updateParams);<br>
<br>
}<br>
public function search()<br>
{<br>
$searchParams['index'] = 'my_index';<br>
$searchParams['type'] = 'my_index';<br>
$searchParams['from'] = 0;<br>
$searchParams['size'] = 100;<br>
$searchParams['sort'] = array(<br>
'_score' => array(<br>
'order' => 'desc'<br>
)<br>
);<br>
// $searchParams['body']['query']['match']['testField'] = 'abc';<br>
$retDoc = $this->client->search($searchParams);<br>
print_r($retDoc);<br>
}<br>
<br>
public function get_document()<br>
{<br>
$getParams = array();<br>
$getParams['index'] = 'my_index';<br>
$getParams['type'] = 'my_index';<br>
$getParams['id'] = 'AU4Kn-knWOmOrmyOj2qg';<br>
$retDoc = $this->client->get($getParams);<br>
print_r($retDoc);<br>
}<br>
}<br>
?>
Elasticsearch.zip ( 1.53 MB 下载:266 次 )
AD:真正免费,域名+虚机+企业邮箱=0元