首先引用composer安装客户端
composer require elasticsearch/elasticsearch
<?php
/*
Create By 2022/1/14 - 14:11 - Delimma
To overcome difficulties!
*/
namespace app\xadmin\controller;
use Elasticsearch\ClientBuilder;
use think\Controller;
class Es extends Controller
{
// composer require elasticsearch/elasticsearch
private $client ;
public function __construct()
{
$hosts = [
'121.196.35.102:9200'
];
$this->client = ClientBuilder::create()->setHosts($hosts)
->setRetries(10)->build();
}
/**
* 添加索引以及映射
*/
public function insertDataIntoEs()
{
$params = [
'index' => 'test_index',
'body' => [
'settings' => [ // 分片
'number_of_shards' => 3, // 3个分片
'number_of_replicas' => 2, // 分片副本 2个
],
#通过设置mapping结构创建一个索引库(相当于mysql创建一个数据库)
'mappings' => [
'_source' => [
'enabled' => true , //原数据查找
],
# 文档属性定义
#文档类型设置(相当于mysql的数据类型)
'properties' => [
'name' => [
'type' => 'keyword' // 关键字
],
'age' => [
'type' => 'integer' //整形
],
'mobile' => [
'type' => 'text' // 文本
],
'createtime' => [
'type' => 'date' // 日期
],
'desc' => [
'type' => 'text' ,
'analyzer' => 'ik_max_word' // 分词
]
]
],
]
];
# 创建索引以及文档
$result = $this->client->indices()->create($params);
return $result;
}
/**
* 添加
*/
public function addDataInfoEs()
{
$params = [
'index' => 'test_index',
'id' => '7',
'body' => [
'name' => '张三',
'age' => 20,
'mobile' => '17767749257',
'createtime' => date('Y-m-d'),
'desc' => '这里是张三的描述,PHP 是一种创建并完成动态交互性站点的强有力的服务器端脚本语言。PHP 是免费的,并且使用非常广泛。同时,对于像微软 ASP 这样的竞争者来说,PHP 无疑是另一种高效率的选项。',
]
];
$res = $this->client->index($params);
dump($res);
die;
return $res;
}
/**
* 批量添加
*/
public function batchAddDataInfoEs()
{
$data = [
['name' => '张三', 'age' => 10, 'mobile' => '17767749257', 'createtime' => '2020-12-12', 'desc' => '协程MySQL客户端,但是人家不推荐使用了,现在推荐使用的是Swoole\Runtime::enableCoroutine+PDO或Mysqli方式,即一键协程化原生PHP的MySQL客户端'],
['name' => '李四', 'age' => 20, 'mobile' => '13147649824', 'createtime' => '2020-10-15', 'desc' => '基于hyperf/pool实现数据库连接池并对模型进行了新的抽象,以它作为桥梁,hyperf才能把数据库组件及事件组件接进来'],
['name' => '白兮', 'age' => 15, 'mobile' => '13411588956', 'createtime' => '2020-08-12', 'desc' => 'TypeScript里的类型兼容性是基于结构子类型的。 结构类型是一种只使用其成员来描述类型的方式。 它正好与名义(nominal)类型形成对比。(译者注:在基于名义类型的类型系统中,数据类型的兼容性或等价性是通过明确的声明和/或类型的名称来决定的。这与结构性类型系统不同,它是基于类型的组成结构,且不要求明确地声明。) 看下面的例子'],
['name' => '王五', 'age' => 25, 'mobile' => '18955045536', 'createtime' => '2020-12-01', 'desc' => '在使用基于名义类型的语言,比如C#或Java中,这段代码会报错,因为Person类没有明确说明其实现了Named接口。'],
];
foreach ($data as $k=>$v){
$params['body'][] = [
'index' => [
'_index' => 'test_index',
'_id' => $k+1
],
];
$params['body'][] = [
'name' => $v['name'],
'age' => $v['age'],
'mobile' => $v['mobile'],
'desc' => $v['desc'],
'createtime' => $v['createtime'],
];
}
$res = $this->client->bulk($params);
dump($res);
die;
return $res;
}
/**
* 删除数据
*/
public function delDataInfoEs()
{
$params = [
'index' => 'test_index',
'id' => 'EFndYX4BAQfgVXMQYs3p',
];
$res = $this->client->delete($params);
dump($res);
die;
}
/**
* 删除索引下所有数据
*/
public function delAllDataInfoEs()
{
$params = [
'index' => 'test_index',
];
$res = $this->client->indices()->delete($params);
dump($res);
die;
}
/**
* id查詢
*/
public function findDataInfoEs()
{
$params = [
'index' => 'test_index',
'id' => '1',
];
$res = $this->client->get($params);
return $res;
}
/**
* 排序搜索最大id
*/
public function findMaxIdDataInfoEs()
{
$params = [
'index' => 'test_index',
'body' => [
'query' => [
'match' => [
// 'mobile' => '17767749257', # 全匹配
'desc' => '客端', # 分词器模糊匹配 'analyzer' => 'ik_max_word' // 分词
]
]
]
];
$res = $this->client->search($params);
dump($res);
die;
}
/**
* 模糊搜索
*/
public function searchDataInfoEs()
{
$params = [
'index' => 'test_index',
'body' => [
'query' => [
'match' => [
// 'mobile' => '17767749257', # 全匹配
'desc' => '客端', # 分词器模糊匹配 'analyzer' => 'ik_max_word' // 分词
]
],
'sort' => [
'_score' => ['order'=>'desc']
]
]
];
$res = $this->client->search($params);
dump($res);
die;
}
/**
* 布尔搜索
*/
public function boolSearchDataInfoEs()
{
$params = [
'index' => 'test_index',
'body' => [
'query' => [
'bool' => [
'must' => [
// 查询手机号, 并且姓名=张三
['match' => ['mobile' => '17767749257']],
['match' => ['name' => '张三']]
]
]
]
]
];
$res = $this->client->search($params);
dump($res);
die;
}
/**
* 布尔+过滤器查询
*/
public function boolAndFilterSearchDataInfoEs()
{
$params = [
'index' => 'test_index',
'body' => [
'query' =>[
'bool' => [
'filter' => [
'term' => ['mobile' => '17767749257']
],
'should' => [
'match' => ['name' => '张三']
]
]
]
]
];
$res = $this->client->search($params);
dump($res);
die;
}
/**
* 分页查询
*/
public function getDataInfoEsByPage()
{
$params = [
'scroll' => '30s', // 滚动请求间隔多长时间。应该很小!
'size' => 2 , // 你想要回多少个结果
'index' => 'test_index',
'body' => [
'query' => [
# 查询所有
// 'match_all' => new \stdClass()
# 查询手机号, 并且姓名=张三
'bool' => [
'must' => [
['match' => ['mobile' => '17767749257']],
['match' => ['name' => '张三']]
]
],
],
],
];
$res = $this->client->search($params);
dump($res);
die;
}
/**
* 更新文档
*/
public function updateDataInfoEs()
{
$params = [
'index' => 'test_index',
'id' => 6,
'body' => [
'doc' => [
'mobile' => '17868422241',
'name' => '法外狂徒'
]
]
];
$res = $this->client->update($params);
dump($res);
die;
}
/**
* 自增更新
*/
public function setIncDataInfoEs()
{
//执行一个脚本来进行更新操作,如对字段进行自增操作或添加新字段。
//为了执行一个脚本更新,你要提供脚本命令和一些参数:
$params = [
'index' => 'test_index',
'id' => 6,
'body' => [
'script' => 'ctx._source.age +=20',
]
];
$res = $this->client->update($params);
dump($res);
}
}