PHP使用elasticsearch

zhan11400
字数 14853阅读 90

使用前先composer安装elasticsearch

composer require elasticsearch/elasticsearch

<?php

namespace app\controller;


use app\BaseController;
use Elasticsearch\ClientBuilder;
use think\App;
use think\facade\Db;


class Index extends BaseController
{
    use TestTrait,TestTrait2;
    private $client;
    private $index_name;
    private $type;
    // 构造函数
    public function __construct(App $app)
    {
        parent::__construct($app);
        $params = array(
            '127.0.0.1:9200'
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }
    //设置索引跟 类型
    public function set($index_name='index',$type='type'){
        $this->index_name=$index_name;
        $this->type=$type;
    }

     //mysql涉及到搜索的数据插入文档
    public function sync(){
        $list=Db::name("test")->select();
        foreach ($list as $k => $v) {
              $r = $this->add_doc($v['id'],$v);
              var_dump($r);
        }
    }
    // 搜索
    public function index(){
        //设置索引跟类型
        $this->set('ccc','bbb');
        $r = $this->search_doc("删库 别烦 我 php");
       echo '<pre>';
        print_r($r);
        exit;
     /*   $this->create_index();

        $r=$this->create_mappings();
        $r=$this->get_mapping();
        $this->sync();
        var_dump($r);
        exit;*/
        //创建索引
        //$this->create_index();
        //删除索引
        //$this->delete_index();
        $id=1;
        //文档是否存在
        $r=$this->exists_doc($id);
        if($r===true){
            //获取文档信息
            $r= $this->get_doc($id);
            //修改文档
            $r=$this->update_doc($id,'name','小明3');
            //删除文档
           //    $this->delete_doc('z_-QynEBGL3U9v6G-u4a');
        }
        dd($r);

        $r = $this->search_doc("删库 别烦我");
     //   dd($r);
    }
    // 删除索引
    public function delete_index()
    {
        $params = ['index' => $this->index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
    // 创建索引

    /**
     * @return array|callable
     */
    public function create_index()
    {
        // 只能创建一次
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'body'=>[]
        ];
       return $this->client->index($params);


    }
    //获取文档
    public function get_doc($id){
        $params = [
            'index' =>$this->index_name,
            'type' => $this->type,
            'id'=>$id
        ];
        return  $this->client->get($params);
    }
    // 创建文档模板
    public function create_mappings() {

        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'include_type_name' => true,//7.0以上版本必须有
            'body' => [
                    'properties' => [
                        'id' => [
                            'type' => 'long', // 整型
                        ],
                        'name' => [
                            //5.x以上已经没有string类型。如果需要分词的话使用text,不需要分词使用keyword。
                            'type' => 'text', // 字符串型
                        ],
                        'profile' => [
                            'type' => 'text',
                        ],
                        'age' => [
                            'type' => 'long',
                        ],
                        'job' => [
                            'type' => 'text',
                        ],
                    ]
  ]
        ];

        return $this->client->indices()->putMapping($params);
    }
    // 查看映射
    public function get_mapping() {
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'include_type_name' => true,//7.0以上版本必须有
        ];
        return $this->client->indices()->getMapping($params);
    }

    // 添加文档
    public function add_doc($id,$doc) {
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'id' => $id,
            'body' => $doc
        ];
        return $this->client->index($params);
    }

    // 判断文档存在
    public function exists_doc($id = 1) {
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'id' => $id
        ];
        return $this->client->exists($params);
    }


    // 更新文档
    public function update_doc($id,$key,$value) {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'id' => $id,
            'body' => [
                'doc' => [
                    $key => $value
                ]
            ]
        ];

        return $this->client->update($params);
    }
    // 删除文档
    public function delete_doc($id = 1) {
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'id' => $id
        ];
        return $this->client->delete($params);
    }

    // 查询文档 (分页,排序,权重,过滤)
    public function search_doc($keywords,$from = 0,$size = 12) {
        $params = [
            'index' => $this->index_name,
            'type' => $this->type,
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'profile' => [
                                'query' => $keywords,
                                'boost' => 3, // 权重大
                            ]]],
                            [ 'match' => [ 'name' => [
                                'query' => $keywords,
                                'boost' => 2,
                            ]]],
                        ],
                    ],
                ],
              //  'sort' => ['age'=>['order'=>'desc']],
                 'from' => $from, 'size' => $size
            ]
        ];

        return $this->client->search($params);
    }

}


0条评论
    暂无评论
发表评论