前提:已经安装好ES跟iK中文分析器。
进入你的TP5.1项目根目录 运行:composer require elasticsearch/elasticsearch

这是封装的对es基本操作
创建索引—创建模板----添加文档 等操作
```php
<?php
namespace app\search\controller;
use Elasticsearch\ClientBuilder;
require '../vendor/autoload.php';
/**
* elasticsearch 示例
*/
class Test{
private $client;
// 构造函数 建立链接
public function __construct(){
$hosts = ['127.0.0.1:9200'];
$this->client = ClientBuilder::create()
->setHosts($hosts)->build();
}
/**
* 是否存在索引
* @index_name 索引名称
*/
public function exists_index($index_name){
$params = [
'index' => $index_name,
];
$response = $this->client->indices()->exists($params);
return $response;
}
/**
* 创建索引 (创建表)
* @index_name 索引名称
*/
public function create_index($index_name){
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 5, // 数据分片数 5
'number_of_replicas' => 0 // 数据备份数 0
]
],
];
try {
return $this->client->indices()->create($params);
} catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
var_dump($e);exit;
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
/**
* 删除索引
* @index_name 索引名称
*/
public function delete_index($index_name){
$params = ['index'=>$index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
/**
* 创建索引模板(表结构)
*@index_name 索引名称
* @type_name 类型名称
*/
public function create_dir($index_name,$type_name){
$params = [
'index' => $index_name,
'type'=> $type_name,
'body' => [
'_source' => [
'enabled' => true
],
'properties' => [
'shop_id' => [
'type' => 'integer', // 整型
],
'shop_name' => [
'type' => 'text', // 字符串型
'analyzer' => 'ik_max_word'
],
'class_name' => [
'type' => 'keyword'
],
'shop_title' => [
'type' => 'text', // 字符串型
'analyzer' => 'ik_max_word'
],
'shop_content' => [
'type' => 'text', // 字符串型
'analyzer' => 'ik_max_word'
],
'shop_price'=>[
'type'=>'scaled_float',
"scaling_factor"=>100,
],
'shop_date'=>[
'type'=>'date',
]
]
],
'include_type_name'=>true,
];
$response = $this->client->indices()->putMapping($params);
return $response;
}
/**
* 查看映射
*@index_name 索引名称
* @type_name 类型名称
*/
public function get_mapping($index_name,$type_name) {
$params = [
'index' => $index_name,
'type' => $type_name,
'include_type_name'=>true
];
$response = $this->client->indices()->getMapping($params);
return $response;
}
/**
* 添加数据
* @index_name 索引名称
* @type_name 类型名称
* @id 数据id
* @content 数据
*/
public function add_file($index_name,$type_name,$id,$content){
$params = [
'index' => $index_name,
'type' => $type_name,
'id'=>$id,
'body'=>$content
];
$response = $this->client->index($params);
return $response;
}
/**
* 判断文档是否存在
* @index_name 索引名称
* @type_name 类型名称
* @id 数据id
*/
public function exists_file($index_name,$type_name,$id){
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->exists($params);
return $response;
}
/**
* 获取文档
* @index_name 索引名称
* @type_name 类型名称
* @id 数据id
*/
public function get_file($index_name,$type_name,$id) {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->get($params);
return $response;
}
/**
* 更新数据
* @index_name 索引名称
* @type_name 类型名称
* @id 数据id
* @content 数据
*/
pub

最低0.47元/天 解锁文章
1461

被折叠的 条评论
为什么被折叠?



