ElasticSearch(六) Elasticsearch在Thinkphp5.0中的使用

首先下载需要引入的类库

链接:https://pan.baidu.com/s/1XEXviLoWM-ypwJ_B0jXqlg 密码:u54t    //Elasticsearch.zip类库压缩包地址

然后将压缩包解压到vendor目录下

 

<?php
namespace appindexcontroller;
use thinkController;
class Ec extends Controller
{
    public function _initialize()
    {
     
        Vendor('Elasticsearch.autoload');
        $params['hosts'] = array(
            '192.168.9.155:9200'
        );
        $this->client = new ElasticsearchClient($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);
    }
}
?>

关于更多详细的说明就自己百度一下官方文档和百度其他帖子吧~

原文地址:https://www.cnblogs.com/wt645631686/p/8275279.html