TP5.1使用ES示例

前提:已经安装好ES跟iK中文分析器。
进入你的TP5.1项目根目录 运行:composer require elasticsearch/elasticsearch
在这里插入图片描述
这是封装的对es基本操作
创建索引—创建模板----添加文档 等操作


```php
<?php
namespace app\search\controller;

use Elasticsearch\ClientBuilder;

require '../vendor/autoload.php';


/**
 *   elasticsearch 示例
 */
class Test{
    private $client;
    // 构造函数 建立链接
    public function __construct(){
        $hosts  = ['127.0.0.1:9200'];
        $this->client = ClientBuilder::create()
                ->setHosts($hosts)->build();
    }
    /**
     * 是否存在索引
     * @index_name 索引名称
    */
    public function exists_index($index_name){
        $params = [
            'index' => $index_name,
        ];
        $response = $this->client->indices()->exists($params);
        return $response;
    }
    /**
     * 创建索引 (创建表)
     * @index_name 索引名称
    */
    public  function create_index($index_name){
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,  // 数据分片数 5
                    'number_of_replicas' => 0 // 数据备份数 0
                ]
            ],
        ];
        try {
            return $this->client->indices()->create($params);
        } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            var_dump($e);exit;
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }

    /**
     * 删除索引
     * @index_name 索引名称
    */
    public function delete_index($index_name){
        $params = ['index'=>$index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
    /**
     * 创建索引模板(表结构)
     *@index_name 索引名称
     * @type_name 类型名称
    */
    public function create_dir($index_name,$type_name){
        $params = [
            'index' => $index_name,
            'type'=> $type_name,
            'body' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'shop_id' => [
                        'type' => 'integer', // 整型
                    ],
                    'shop_name' => [
                        'type' => 'text', // 字符串型
                        'analyzer' => 'ik_max_word'
                    ],
                    'class_name' => [
                        'type' => 'keyword'
                    ],
                    'shop_title' => [
                        'type' => 'text', // 字符串型
                        'analyzer' => 'ik_max_word'
                    ],
                    'shop_content' => [
                        'type' => 'text', // 字符串型
                        'analyzer' => 'ik_max_word'
                    ],
                    'shop_price'=>[
                        'type'=>'scaled_float',
                        "scaling_factor"=>100,
                    ],
                    'shop_date'=>[
                        'type'=>'date',
                    ]
                ]
            ],
            'include_type_name'=>true,

        ];
        $response = $this->client->indices()->putMapping($params);
        return $response;
    }
    /**
     * 查看映射
     *@index_name 索引名称
     * @type_name 类型名称
    */
    public function get_mapping($index_name,$type_name) {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'include_type_name'=>true
        ];
        $response = $this->client->indices()->getMapping($params);
        return $response;
    }
    /**
     * 添加数据
     * @index_name 索引名称
     * @type_name 类型名称
     * @id 数据id
     * @content 数据
    */
    public function add_file($index_name,$type_name,$id,$content){
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id'=>$id,
            'body'=>$content
        ];
        $response = $this->client->index($params);
        return $response;
    }
    /**
     * 判断文档是否存在
     *  @index_name 索引名称
     * @type_name 类型名称
     * @id 数据id
    */
    public function exists_file($index_name,$type_name,$id){
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->exists($params);
        return $response;
    }
    /**
     * 获取文档
     *  @index_name 索引名称
     * @type_name 类型名称
     * @id 数据id
     */
    public function get_file($index_name,$type_name,$id) {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->get($params);
        return $response;
    }
    /**
     * 更新数据
     * @index_name 索引名称
     * @type_name 类型名称
     * @id 数据id
     * @content 数据
     */
    pub
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值