最新php中tp5使用es(Elasticsearch7.14使用篇)

5 篇文章 0 订阅

中文参考文档

安装篇

1、连接es

use Elasticsearch\ClientBuilder; 
private  $client;
    public function __construct()
    {
        $this->client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();

    }

2、创建索引(相当mysql数据库)

  index为索引名字  分片讲解

    //创建索引
    //现在我们开始添加一个新的索引和一些自定义设置:
    public function create_index()
    {
        $params = [
            'index' => 'myindex', #index的名字不能是大写和下划线开头
            'body' => [
                'settings' => [
                    'number_of_shards' => 1,  //分片
                    'number_of_replicas' => 0 //副本
                ],  
            ]
        ];
        $client=  $this->client->indices()->create($params);
        dump($client);exit;
    }

3、设置索引字段

enabled讲解  

 //设置字段
    public function add_filed()
    {

        $params = [
            'index' => "myindex",
            'body' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' =>
                    [
                        "product_name"=>[
                            "type"=>'text',
                        ],
                        'prodcut_id'=>[
                            "type"=>"integer"
                        ]
                    ]
            ]
        ];

        $client = $this->client->indices()->putMapping($params);
        dump($client);exit;
    }

4、获取已设置索引字段

  //获取字段
    public function get_filed()
    {
        $post = "myindex";
        $params['index'] = $post['index'];
        $res = $this->client->indices()->getMapping($params);
        $data = isset($res[$post['index']]['mappings']['properties']) ? $res[$post['index']]['mappings']['properties'] : [];
        $return=[];
        $i=0;
        foreach ($data as $key=>$value){
            ++$i;
            $return[]=[
                "name"=>$key,
                "type"=>$value['type'],
                "analyzer"=>isset($value['analyzer'])?$value['analyzer']:""
            ];
        }
       dump($return);
    }

5、删除索引

    //删除索引
    public function delete_index()
    {
        $deleteParams['index'] = 'myindex';
        $this->client->indices()->delete($deleteParams);
    }

6、修改索引配置


    //修改索引配置
    public function update_index()
    {
        $post = "myindex";

        $params = [
            'index' => $post['index'], #index的名字不能是大写和下划线开头
            'body' => [
                'settings' => [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0
                ]
            ]
        ];
 
     unset($params['body']['settings']['number_of_shards']);
      $this->client->indices()->putSettings($params);
     
    }

7、插入文档

    //插入索引数据
    public function add_document()
    {
        $params = array();
        $params['body'] = array(
            'product_name' => '要插入的商品名称',
            'prodcut_id' => 5
        );
        $params['index'] = 'myindex';  //索引名称
        $params['type'] = '_doc';   //类型名称
        $params['id'] = '12345678';     //不指定id,系统会自动生成唯一id
        $ret = $this->client->index($params);
    }

8、修改文档

    //更改文档
    public function update_document()
    {
        $updateParams = array();
        $updateParams['index'] = 'myindex';
        $updateParams['type'] = '_doc';
        $updateParams['id'] = 'my_id';
        $updateParams['body']['doc']['product_name']  = '新商品名';
        $response = $this->client->update($updateParams);

    }

9、删除文档

    //删除文档
    public function delete_document()
    {
        $deleteParams = array();
        $deleteParams['index'] = 'myindex';
        $deleteParams['type'] = '_doc';
        $deleteParams['id'] = '123';
        $retDelete = $this->client->delete($deleteParams);
    }

10、获取文档

    //获取文档
    public function get_document()
    {
        $getParams = array();
        $getParams['index'] = 'myindex';
        $getParams['type'] = '_doc';
        $getParams['id'] = '12344';
        $retDoc = $this->client->get($getParams);
        print_r($retDoc);
    }

11、文档查询

 //查询
    public function search()
    {
        $searchParams['index'] = 'myindex';
        $searchParams['type'] = '_doc';
        $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;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP隔壁老王邻居

啦啦啦啦啦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值