首先下載需要引入的類庫
連結: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();
}
//建立索引
//現在我們開始新增一個新的索引和一些自定義設定:
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);
}
//插入索引資料
public function add_document()
{
$params = array();
$params['body'] = array(
'product_name' => '要插入的商品名稱',
'prodcut_id' => 5
);
$params['index'] = 'myindex'; //索引名稱
$params['type'] = 'mytype'; //型別名稱
$params['id'] = '12345678'; //不指定id,系統會自動生成唯一id
$ret = $this->client->index($params);
}
//刪除索引
//由於 elasticsearch 的動態性質,我們新增第一個文件的時候自動建立了索引和一些預設設定。讓我們刪除這個索引,因為我們以後想要指定自己的設定:
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);
}
}
?>
關於更多詳細的說明就自己百度一下官方文件和百度其他帖子吧~