tp5对接elasticsearch7.17.6实现全文检索demo

 <?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
# 引入开发包 elasticsearch
use Elastic\Elasticsearch\ClientBuilder;

/**
 * elasticsearch 全文检索
 */
class Search extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    private $client;
    
    // 构造函数
    public function __construct()
    {
        $params = array(
            '127.0.0.1:9200'
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
        parent::__construct();
    }

    /**
     *  1.创建索引
     *
     * @return void
     */
    public function p1(){
        $this->create_index(); 
    }

    /**
     * 2.创建文档模板
     *
     * @return void
     */
    public function p2(){
        $this->create_mappings(); 
    }

    /**
     * 3.添加数据
     *
     * @return void
     */
    public function p3(){
        $docs = [];
        $docs[] = ['id'=>1,'name'=>'小明','profile'=>'我做的ui界面强无敌。','age'=>23];
        $docs[] = ['id'=>2,'name'=>'小张','profile'=>'我的php代码无懈可击。','age'=>24];
        $docs[] = ['id'=>3,'name'=>'小王','profile'=>'C的生活,快乐每一天。','age'=>29];
        $docs[] = ['id'=>4,'name'=>'小赵','profile'=>'就没有我做不出的前端页面。','age'=>26];
        $docs[] = ['id'=>5,'name'=>'小吴','profile'=>'php是最好的语言。','job'=>21];
        $docs[] = ['id'=>6,'name'=>'小翁','profile'=>'别烦我,我正在敲bug呢!','age'=>25];
        $docs[] = ['id'=>7,'name'=>'小杨','profile'=>'为所欲为,不行就删库跑路','age'=>27];
 
        foreach ($docs as $k => $v) {
            $r = $this->add_doc($v['id'],$v);   
        }
    }

    /**
     * 4.搜索
     *
     * @return void
     */
    public function p4(){
        $r = $this->search_doc("删库别烦我为所欲为php");
        //搜索结果
        halt($r['hits']['hits']);
        // $r = $this->get_doc();
        // halt($r);
    }
 
    /**
     * 创建索引
     *
     * @param string $index_name
     * @return void
     */
    public function create_index($index_name = 'test_ik') { // 只能创建一次
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 0
                ]
            ]
        ];
 
        try {
            return $this->client->indices()->create($params);
        } catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }
 
    /**
     * 删除索引
     *
     * @param string $index_name
     * @return void
     */
    public function delete_index($index_name = 'test_ik') {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
 
    /**
     *  创建文档模板
     *
     * @param string $type_name
     * @param string $index_name
     * @return void
     */
    public function create_mappings($type_name = 'users',$index_name = 'test_ik') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'include_type_name' => true,
            'body' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'id' => [
                        'type' => 'integer', // 整型
                        'index' => false,
                    ],
                    'name' => [
                        'type' => 'text', // 字符串型
                        'index' => true, // 全文搜索
                        'analyzer' => 'ik_max_word'
                    ],
                    'profile' => [
                        'type' => 'text',
                        'index' => true,
                        'analyzer' => 'ik_max_word'
                    ],
                    'age' => [
                        'type' => 'integer',
                    ],
                ]
            ]
        ];
 
        $response = $this->client->indices()->putMapping($params);
        return $response;
    }
 
    /**
     * 查看映射
     *
     * @param string $type_name
     * @param string $index_name
     * @return void
     */
    public function get_mapping($type_name = 'users',$index_name = 'test_ik') {
        $params = [
            'index' => $index_name,
            'type' => $type_name
        ];
        $response = $this->client->indices()->getMapping($params);
        return $response;
    }
 
    /**
     * 添加文档
     *
     * @param [type] $id     
     * @param [type] $doc
     * @param string $index_name
     * @param string $type_name
     * @return void
     */
    public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'users') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $doc
        ];
 
        $response = $this->client->index($params);
        return $response;
    }
 
    /**
     * 判断文档存在
     *
     * @param integer $id
     * @param string $index_name
     * @param string $type_name
     * @return void
     */
    public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
 
        $response = $this->client->exists($params);
        return $response;
    }
 
 
    /**
     * 获取文档
     *
     * @param integer $id
     * @param string $index_name
     * @param string $type_name
     * @return void
     */
    public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
 
        $response = $this->client->get($params);
        return $response;
    }
 
    /**
     *  更新文档
     *
     * @param integer $id
     * @param string $index_name
     * @param string $type_name
     * @return void
     */
    public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => [
                'doc' => [
                    'name' => '大王'
                ]
            ]
        ];
 
        $response = $this->client->update($params);
        return $response;
    }
 
    /**
     * 删除文档
     *
     * @param integer $id
     * @param string $index_name
     * @param string $type_name
     * @return void
     */
    public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
 
        $response = $this->client->delete($params);
        return $response;
    }
 
    /**
     * 查询文档 (分页,排序,权重,过滤)
     *
     * @param string $keywords
     * @param string $index_name
     * @param string $type_name
     * @param integer $from
     * @param integer $size
     * @return void
     */
    public function search_doc($keywords = "运维",$index_name = "test_ik",$type_name = "users",$from = 0,$size = 10) {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'profile' => [
                                'query' => $keywords,
                                'boost' => 3, // 权重大
                            ]]],
                            [ 'match' => [ 'name' => [
                                'query' => $keywords,
                                'boost' => 2,
                            ]]],
                        ],
                    ],
                ],
                'sort' => ['age'=>['order'=>'desc']]
                , 'from' => $from, 'size' => $size
            ]
        ];
 
        $results = $this->client->search($params);
        return $results;
    }

    
}

本文由 来鹏飞 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论