thinkphp8 Elasticsearch7.15 使用及基本查询

安装扩展

composer require elasticsearch/elasticsearch 7.15

操作ES客户端类

<?php

namespace common;

use Elasticsearch\ClientBuilder;
use think\facade\Db;

class Elasticsearch
{
    private $client;
    private $indexName;
    private $type;

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

    /**
     * 设置索引与类型
     * @param string $indexName
     * @param string $type
     */
    public function setIndex($indexName = 'index', $type = 'type')
    {
        $this->indexName = $indexName;
        $this->type = $type;
    }

    /**
     * mysql涉及到搜索的数据插入文档
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sync()
    {
        $list = Db::name("test")->select();
        foreach ($list as $k => $v) {
            $r = $this->addDoc($v['id'], $v);
        }
    }

    /**
     * 查看索引是否存在
     */
    public function existsIndex(){
        $params = ['index' => $this->indexName];
        return $this->client->indices()->exists($params);
    }

    /**
     * 删除索引
     *
     * @return void
     */
    public function deleteIndex()
    {
        $params = ['index' => $this->indexName];
        return $this->client->indices()->delete($params);
    }

    /**
     * 创建索引
     */
    public function createIndex()
    {
        // 只能创建一次
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'body' => []
        ];
        return $this->client->index($params);
    }

    /**
     * 获取文档
     *
     * @param [type] $id
     * @return void
     */
    public function getDoc($id)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'id' => $id
        ];
        return  $this->client->get($params);
    }

    /**
     * 创建文档结构
     *
     * @param [type] $id
     * @param [type] $data
     * @return void
     */
    public function createMappings($body)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'include_type_name' => true, //7.0以上版本必须有
            'body' => $body
        ];

        return $this->client->indices()->putMapping($params);
    }

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

    /**
     * 添加文档
     * @param string $id
     * @param array $doc 跟创建文档结构时properties的字段一致
     * @return array|callable
     */
    public function addDoc(string $id, array $doc)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'id' => $id,
            'body' => $doc
        ];
        return $this->client->index($params);
    }

    /**
     * 批量添加文档
     * @param $body
     * @return array
     */
    public function addBulkDoc(array $body)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'body' => $body
        ];
        return $this->client->bulk($params);
    }

    /**
     * 判断文档存在
     *
     * @param integer $id
     * @return void
     */
    public function existsDoc($id = 1)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'id' => $id
        ];
        return $this->client->exists($params);
    }

    /**
     * 更新文档
     * @param $id
     * @param $key
     * @param $value
     * @return array|callable
     */
    public function updateDoc($id, $key, $value)
    {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'id' => $id,
            'body' => [
                'doc' => [
                    $key => $value
                ]
            ]
        ];

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

    /**
     * 删除文档
     * @param int $id
     * @return array|callable
     */
    public function deleteDoc($id = 1)
    {
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'id' => $id
        ];
        return $this->client->delete($params);
    }

    /**
     * 查询表达式搜索
     * @param $keywords
     * @param $from
     * @param $size
     * @return array
     */
    public function searchDoc1($keywords, $from, $size)
    {
        return [
            'query' => [
                "match" => [
                    //"name"=>$keywords, 或者
                    "name" => [
                        'query' => $keywords,
                        'boost' => 3, // 权重
                    ],
                ],
            ],
            'from' => $from,
            'size' => $size
        ];
    }

    /**
     * 短语搜索
     * @param $keywords
     * @param $from
     * @param $size
     * @return array
     */
    public function searchDoc3($keywords, $from, $size)
    {
        return [
            'query' => [
                "match_phrase" => [
                    //"name"=>$keywords, 或者
                    "name" => [
                        'query' => $keywords,
                        'boost' => 3, // 权重
                    ],
                ],


            ],
            'from' => $from,
            'size' => $size
        ];
    }

    /**
     * 高亮搜索
     * @param string $keywords
     * @param $from
     * @param $size
     * @return array
     */
    public function searchDoc4(string $keywords, $from, $size): array
    {
        return [
            'query' => [
                "match_phrase" => [
                    //"name"=>$keywords, 或者
                    "name" => [
                        'query' => $keywords,
                        'boost' => 3, // 权重
                    ],
                ],
            ],
            'highlight' => [
                "fields" => [
                    //必须加object,要不然转json时,这里依然是数组,而不是对象
                    "name" => (object)[]
                ]
            ],
            'from' => $from,
            'size' => $size
        ];
    }

    /**
     * 搜索结果增加分析
     * @param string $keywords
     * @param int $from
     * @param int $size
     * @return array
     */
    public function searchDoc5(string $keywords = '', int $from = 0, int $size = 12)
    {
        return [
            'query' => [
                'bool' => [
                    //必须匹配
                    "must" => [
                        "match" => [
                            "profile" => $keywords,
                        ]
                    ],
                    //应该匹配
                    'should' => [
                        ['match' => [
                            'profile' => [
                                'query' => $keywords,
                                'boost' => 3, // 权重
                            ]
                        ]],
                        ['match' => ['name' => [
                            'query' => $keywords,
                            'boost' => 2,
                        ]]],
                    ],
                    //复杂的搜索 限制年龄大于25岁
                    'filter' => [
                        "range" => [
                            "age" => ["gt" => 25]
                        ]
                    ]
                ],
            ],
            'highlight' => [
                "fields" => [
                    //必须加object,要不然转json时,这里依然是数组,而不是对象
                    "name" => (object)[]
                ]
            ],
            'aggs' => [
                "result" => [
                    //terms 桶 统计文档数量
                    "terms" => [
                        "field" => "age"
                    ]
                ],
                "avg" => [
                    //avg 平均值
                    "avg" => [
                        "field" => "age"
                    ]
                ],
                "max" => [
                    //max 最大值
                    "max" => [
                        "field" => "age"
                    ]
                ],
                "min" => [
                    //avg 最小值
                    "min" => [
                        "field" => "age"
                    ]
                ],
            ],
            'from' => $from,
            'size' => $size,
        ];
    }

    /**
     * 使用过滤器 filter
     * @param $keywords
     * @param $from
     * @param $size
     * @return array
     */
    public function searchDoc2($keywords, $from, $size)
    {
        return  [
            'query' => [
                'bool' => [
                    //必须匹配
                    "must" => [
                        "match" => [
                            "name" => $keywords,
                        ]
                    ],
                    //应该匹配
                    'should' => [
                        ['match' => [
                            'profile' => [
                                'query' => $keywords,
                                'boost' => 3, // 权重
                            ]
                        ]],
                        ['match' => ['name' => [
                            'query' => $keywords,
                            'boost' => 2,
                        ]]],
                    ],
                    //复杂的搜索 限制年龄大于25岁
                    'filter' => [
                        "range" => [
                            "age" => ["gt" => 25]
                        ]
                    ]
                ],

            ],
            //  'sort' => ['age'=>['order'=>'desc']],
            'from' => $from,
            'size' => $size
        ];
    }

    /** 
     * 查询文档 (分页,排序,权重,过滤)
     * @param $keywords
     * @param int $from
     * @param int $size
     * @return array|callable
     */
    public function searchDoc($keywords, $from = 0, $size = 12)
    {
        $query = $this->searchDoc5($keywords, $from, $size);
        $params = [
            'index' => $this->indexName,
            'type' => $this->type,
            'body' => $query
        ];

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

具体使用案例

<?php

namespace app\controller;

use app\BaseController;
use common\Elasticsearch;

class Index extends BaseController
{
    public $elasticsearch;

    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->elasticsearch = new Elasticsearch();
    }

    public function index()
    {   
        $index = 'user';
        $type = 'user_';
        //设置索引
        $this->elasticsearch->setIndex($index,$type);

        //判断索引是否存在,不存在则创建
        if(!$this->elasticsearch->existsIndex())
        {
            $this->elasticsearch->createIndex();
        }

        //获取映射 如果为空则创建
        $mapping = $this->elasticsearch->getMapping();
        if(empty($mapping[$index]['mappings'][$type])){
            $params = [
                'properties' => [
                    'id' => [
                        'type' => 'long', // 整型
                    ],
                    'name' => [
                        //5.x以上已经没有string类型。如果需要分词的话使用text,不需要分词使用keyword。
                        'type' => 'text', // 字符串型
                    ],
                    'profile' => [
                        'type' => 'text',
                    ],
                    'age' => [
                        'type' => 'long',
                    ],
                    'job' => [
                        'type' => 'text',
                    ],
                ]
            ];
            $this->elasticsearch->createMappings($params);
        }
        
        //添加文档
        // $data = [
        //     'id' => 5,
        //     'name' => '张三',
        //     'profile' => '我是张三',
        //     'age' => 30,
        //     'job' => '程序员',
        // ];
        // $this->elasticsearch->addDoc(4,$data);

        $arr=[];
        for ($i=6;$i< 20;$i++){
            $arr[]=[
                'id' => $i,
                'name' => '张三_'. $i,
                'profile' => '我是张三_'. $i,
                'age' => 30 + $i,
                'job' => '程序员_'. $i,
            ];
        }
        $body=[];
        foreach ($arr as $k=>$v){
            $body[($k*2)] = [
                'index' => [ 
                    '_id' => $v['id'].'_'.$v['age'],
                    '_type'=> $type,
                    '_index'=> $index
                ]
            ];
            $body[(($k*2)+1)] = $v;
        }
        //$this->elasticsearch->addBulkDoc($body);
        
        

        //获取文档
        $data = $this->elasticsearch->searchDoc('张',0,30);
        var_dump($data['hits']['hits']);
        //var_dump($data['aggregations']['min']);

        
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ThinkPHP6 使用 ElasticSearch 需要先安装 ElasticSearchElasticsearch for PHP,具体安装步骤请参考前面的回答。 安装完成后,我们可以在代码使用 Elasticsearch for PHP 提供的 API 进行数据的增删改查操作。下面是一个简单的示例: ```php <?php namespace app\controller; use Elasticsearch\ClientBuilder; use think\facade\Db; class Index { public function search() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'match' => [ 'title' => 'ElasticSearch' ] ] ] ]; $response = $client->search($params); return json($response); } public function add() { $data = Db::table('my_table')->find(); $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => $data['id'], 'body' => [ 'title' => $data['title'], 'content' => $data['content'], ] ]; $response = $client->index($params); return json($response); } public function update() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'doc' => [ 'title' => 'new title', ] ] ]; $response = $client->update($params); return json($response); } public function delete() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); return json($response); } } ``` 这里我们定义了四个方法,分别是 `search`、`add`、`update` 和 `delete`。 `search` 方法用于查询数据,我们在查询使用了 `match` 查询查询了 `title` 字段包含 `ElasticSearch` 关键字的文档。 `add` 方法用于添加数据,我们使用了 `index` 方法,将 `my_table` 表的数据添加到了名为 `my_index`,类型为 `my_type` 的文档。 `update` 方法用于更新数据,我们使用了 `update` 方法,将 ID 为 `my_id` 的文档的 `title` 字段更新为了 `new title`。 `delete` 方法用于删除数据,我们使用了 `delete` 方法,删除了 ID 为 `my_id` 的文档。 这只是一个简单的示例,实际使用需要根据业务需求进行更详细的配置和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

swoole~

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值