ElasticSearch全文搜索引擎 ,是一款很好的搜索框架!用来做网站的搜索,可以对数据库压力起到一定的缓解作用!
之前给大家带来的是用curl来实现的大家有兴趣可以看看
http://www.thinkphp.cn/topic/29297.html
今天这里给大家带来的是官网提供的 PHP API
整合thinkphp3.2
下载文件解压到
ThinkPHPLibraryVendor<?php <br />
命名空间 HomeController;<br>
<br>
使用 ThinkController;<br>
<br>
EcController 类扩展了 Controller<br>
{<br>
<br>
公共函数_initialize()<br>
{<br>
Vendor('Elasticsearch.autoload');<br>
$params['hosts'] = 数组(<br>
'127.0.0.1:9200'<br>
);<br>
$this->client = new ElasticsearchClient($params);<br>
}<br>
<br>
公共函数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>
公共函数 add_document()<br>
{<br>
$params = array();<br>
$params['body'] = 数组(<br>
'测试字段' => '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>
公共函数delete_index()<br>
{<br>
$deleteParams['index'] = 'my_index';<br>
$this->client->indices()->delete($deleteParams);<br>
}<br>
<br>
公共函数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>
公共函数 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>
公共函数 search()<br>
{<br>
$searchParams['index'] = 'my_index';<br> $searchParams['type'] = 'my_index';<br>
$searchParams['from'] = 0;<br>
$searchParams['size'] = 100;<br>
$searchParams['sort'] = 数组(<br>
'_score' =>; 数组(<br>
'订单' => '描述'<br>
)<br>
);<br>
// $searchParams['body']['query']['match']['testField'] = 'abc';<br>
$retDoc = $this->client->search($searchParams);<br>
print_r($retDoc);<br>
}<br>
<br>
公共函数 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元