前提:已经安装好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 数据
*/
public function update_doc($index_name,$type_name,$id,$content) {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => [
'doc' => $content
]
];
$response = $this->client->update($params);
return $response;
}
/**
* 删除文档
* @index_name 索引名称
* @type_name 类型名称
* @id 数据id
*/
public function delete_file($index_name,$type_name,$id){
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->indices()->delete($params);
return $response;
}
/**
* 搜索文档
*/
public function search_file($index_name,$type_name,$body){
$params = [
'index' => $index_name,
'type' => $type_name,
'body'=> [
'query'=>$body
],
'size'=>2
];
$response = $this->client->search($params);
return $response;
}
<?php
namespace app\search\controller;
use app\search\controller\Test;
class Shop{
private $index_name = "shop";
private $type_name = "shopType";
public function test(){
$Test = new Test();
$Test->delete_index($this->index_name);
if($Test->exists_index($this->index_name) === false) {
$Test->create_index($this->index_name);
}
$Test->create_dir($this->index_name,$this->type_name);
$Test->get_mapping($this->index_name,$this->type_name);
}
public function add(){
$Test = new Test();
/* $shop = $this->getValue();
foreach($shop as $k=>$v) {
$Test->add_file($this->index_name, $this->type_name, $v['shop_id'], $v);
}*/
var_dump($Test->get_file($this->index_name, $this->type_name, 1));exit;
}
public function search(){
$Test = new Test();
$body = [
"bool"=>[
"should"=>[
["match"=>["shop_name"=>"我生"]],
["match"=>["shop_title"=>"我生"]],
["match"=>["shop_content"=>"我生"]]
]
]
];
var_dump($Test->search_file($this->index_name,$this->type_name,$body));
}
private function getValue(){
$arrs = array();
for($i=1;$i<40;$i++){
$arr['shop_id'] = $i;
$arr['shop_name'] = $this->randomChinese(0,4);
$arr['class_name'] = "包包";
$arr['shop_title'] = $this->randomChinese(0,10);
$arr['shop_content'] = $this->randomChinese(0,25);
$arr['shop_price'] = $this->randomFloat(0,1000);
$arr['shop_date'] = $this->randomDate("2020-01-02", "2021-03-03");
$arrs[] = $arr;
}
return $arrs;
}
private function randomChinese($star,$length){
$str = "那是一个非常重要的日子,天气很好,阳光明媚,似乎代表着有好事发生,因为那天是我去学校领小学最后一份成绩报告单的日子。等了七天,所有人都期待着";
$str = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
shuffle($str);
$str = array_slice($str, $star,$length);
$str = implode('', $str);
return $str;
}
private function randomFloat($min, $max)
{
$num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
return sprintf("%.2f", $num);
}
private function randomDate($begintime, $endtime="") {
$begin = strtotime($begintime);
$end = $endtime == "" ? mktime() : strtotime($endtime);
$timestamp = rand($begin, $end);
return date("Y-m-d", $timestamp);
}
}
PHP-ES 中文文档地址链接:https://learnku.com/docs/elasticsearch-php/6.0/index-operations/2007
ES 文档地址链接:https://www.elastic.co/guide/cn/elasticsearch/guide/current/foreword_id.html