thinkphp6 使用 elasticsearch 7.5

前言

记录下thinkphp6 使用 elasticsearch 7.5的方法

安装elasticsearch

在 composer.json 文件中增加配置

 "require": {
        "php": ">=7.1.0",
        "topthink/framework": "^6.0.0",
        "topthink/think-orm": "^2.0",
        "elasticsearch/elasticsearch": "7.5" //我用的是es7.5版本
    }

执行更新composer命令

compser update

使用elasticsearch

<?php


namespace app\controller;

use Elasticsearch\ClientBuilder;


class elasticsearch
{

    //官方文档地址:https://www.elastic.co/guide/cn/elasticsearch/php/current/_overview.html

    protected $client;

    public function __construct()
    {
        $hosts = ['127.0.0.1:9200'];
        $this->client  =  ClientBuilder::create()->setHosts($hosts)->build();;
    }

    //新增文档
    public function addDoc()
    {
        $params = [
            'index' => 'luoaotian',
            'type' => 'user',
            'id' => 1,
            'body' => [
                'name' => '罗傲天',
                'age'  => 28,
                'desc'  => '测试',
                'tag' => ['帅气','温暖']
            ]
        ];
        $response = $this->client->index($params);
        var_dump($response);
    }

    //批量新增文档
    public function addAllDoc()
    {
        $params = ['body' => []];

        for ($i = 1; $i <= 1234567; $i++) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'luoaotian',
                    '_type' => 'user',
                    '_id' => $i
                ]
            ];
            $params['body'][] = [
                'name' => '罗傲天'.$i,
                'age'  => 28,
                'desc'  => '测试'.$i,
                'tag' => ['帅气','温暖']
            ];

            if ($i % 1000 == 0) {
                $responses = $this->client->bulk($params);
                //删除旧的批量请求
                $params = ['body' => []];
                //保存内存后取消设置批量响应
                unset($responses);
            }
        }

        // 发送最后一批(如果存在)
        if (!empty($params['body'])) {
            $responses = $this->client->bulk($params);
        }
        var_dump($responses);
    }


    //删除文档
    public function delDoc()
    {
        $params = [
            'index' => 'luoaotian',
            'type' => 'user',
            'id' => '1'
        ];

        $response = $this->client->delete($params);

        var_dump($response);
    }

    //更新文档
    public function updateDoc()
    {
        $params = [
            'index' => 'luoaotian',
            'type' => 'user',
            'id' => 1,
            'body' => [
                'doc' => [
                    'name' => '罗傲天3',
                    'age'  => 22,
                    'desc'  => '测试1',
                    'tag' => ['帅气','温暖']
                ]

            ]
        ];

        $response = $this->client->update($params);

        var_dump($response);
    }


    //搜索文档
    public function searchDoc()
    {

        $params = [
            'index' => 'luoaotian',//索引名(数据库名)
            'type' => 'user',  //类型名(表名)
            'body' => [
                'query' => [
                   'match' => [
                       'name' => '罗傲天'
                   ]
                ],
                '_source' => ['name','age'],
                'from' => 0,
                'size' => 2,
                'highlight' => [
                    'pre_tags' => "<b class='key' style='color:red'>",
                    'post_tags' => "</b>",
                    'fields' => ['name'=> new \stdClass() ]  //空数组要new一个

                ]
            ]
        ];

        $response = $this->client->search($params);
        var_dump($response['hits']);

    }


}
添加新评论

-->