首先下載需要引入的類庫
鏈接:https://pan.baidu.com/s/1XEXviLoWM-ypwJ_B0jXqlg 密碼:u54t //Elasticsearch.zip類庫壓縮包地址
然后將壓縮包解壓到vendor目錄下
<?php
namespace app\index\controller;
use think\Controller;
class Ec extends Controller
{
public function _initialize()
{
Vendor('Elasticsearch.autoload');
$params['hosts'] = array(
'192.168.9.155:9200'
);
$this->client = new \Elasticsearch\Client($params);
}
public function index(){
$this->search();
}
//創(chuàng)建索引
//現(xiàn)在我們開始添加一個新的索引和一些自定義設置:
public function create_index()
{
$indexParams['index'] = 'myindex'; //索引名稱
$indexParams['type'] = 'mytype'; //類型名稱
$indexParams['body']['settings']['number_of_shards'] = 1; //當前只有一臺ES硬耍,1就可以了
$indexParams['body']['settings']['number_of_replicas'] = 0; //副本0誉裆,因為只有一臺ES
$this->client->create($indexParams);
}
//插入索引數(shù)據(jù)
public function add_document()
{
$params = array();
$params['body'] = array(
'product_name' => '要插入的商品名稱'砚哗,
'prodcut_id' => 5
);
$params['index'] = 'myindex'; //索引名稱
$params['type'] = 'mytype'; //類型名稱
$params['id'] = '12345678'; //不指定id,系統(tǒng)會自動生成唯一id
$ret = $this->client->index($params);
}
//刪除索引
//由于 elasticsearch 的動態(tài)性質(zhì)捉邢,我們添加第一個文檔的時候自動創(chuàng)建了索引和一些默認設置。讓我們刪除這個索引商膊,因為我們以后想要指定自己的設置:
public function delete_index()
{
$deleteParams['index'] = 'myindex';
$this->client->indices()->delete($deleteParams);
}
//刪除文檔
public function delete_document()
{
$deleteParams = array();
$deleteParams['index'] = 'myindex';
$deleteParams['type'] = 'mytype';
$deleteParams['id'] = '123';
$retDelete = $this->client->delete($deleteParams);
}
//更改文檔
public function update_document()
{
$updateParams = array();
$updateParams['index'] = 'myindex';
$updateParams['type'] = 'mytype';
$updateParams['id'] = 'my_id';
$updateParams['body']['doc']['product_name'] = '新商品名';
$response = $this->client->update($updateParams);
}
//查詢
public function search()
{
$searchParams['index'] = 'myindex';
$searchParams['type'] = 'mytype';
$searchParams['from'] = 0;
$searchParams['size'] = 100;
$searchParams['sort'] = array(
'_score' => array(
'order' => 'id'
)
);
//相當于sql語句: select * from hp_product where prodcut_name like '茶' limit 0,100 order by id desc;
$searchParams['body']['query']['match']['product_name'] = '茶';
$retDoc = $this->client->search($searchParams);
echo '<pre>';
print_r($retDoc);
//相當于sql語句: select * from hp_product where product_name like '茶' and product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['must'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
// $searchParams['size'] = 10;
// $searchParams['from'] = 200;
//
//
// 當于sql語句:select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
// $searchParams['body']['query']['bool']['should'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
// 當于sql語句: select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
// $searchParams['body']['query']['bool']['must_not'] = array(
// array('match' => array('product_name' => '茶')),
// array('match' => array('product_id' => 20))
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
//
//
//當于sql語句:select * from hp_product where id>=20 and id<30 limit 200,10;
// $searchParams['body']['query']['range'] = array(
// 'id' => array('gte' => 20,'lt' => 30);
// );
//$searchParams['size'] = 10;
//$searchParams['from'] = 200;
}
//獲取文檔
public function get_document()
{
$getParams = array();
$getParams['index'] = 'myindex';
$getParams['type'] = 'mytype';
$getParams['id'] = '12344';
$retDoc = $this->client->get($getParams);
print_r($retDoc);
}
}
?>