一 使用composer给项目中安装ES
ssh xxx 连接到项目所在的服务器
cd /home/wwwroot/xxx 进入到项目艮目录
composer require elasticsearch/elasticsearch 使用composer安装es
二 创建ES实例
新建一个Esearch.php来进行测试
<?php
namespace app\index\controller;
use Elasticsearch\ClientBuilder;
class Esearch
{
private $client;
public function __construct() {
if (!empty($this->client)) {
return $this->client;
}
$this->client = ClientBuilder::create()->build();
}
三 基本使用
1 创建一个ES索引
核心函数:$this->client->indices()->create($params);
下面的代码创建了一个'mt001'的索引,相当于创建了一个‘mt001’的数据库。还创建了一个‘test’的类型,相当于创建了1个‘test’的表。'body'里面可以放一些基础设置‘settings’,另外放了一个‘mappings’,相当于定义了表结构。
代码如下:
public function create_index() {
$params = [
'index' => 'mt001', //索引名称,相当于数据库的 datebase
// 'type' => 'test', //类型名称,相当于数据库的 tables
// 'id' => 12, //相当于数据库tables中,主键id为12的数据
'body' => [
'settings' => [
'number_of_shards' => 3, //是数据分片数,默认为5,有时候设置为3
'number_of_replicas' => 0, //数据库备份数 如果只有一台机器,设置为0
'analysis' => [ //分析器
'filter' => [
'shingle' => [
'type' => 'shingle'
]
],
'char_filter' => [
'pre_negs' => [
'type' => '