composer安装命令
composer require elasticsearch/elasticsearch
在app目录下创建ES目录及MyEs类
<?php
namespace App\Es;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\Request;
class MyEs
{
//许多文档属于索引 个人理解
protected $es;
//构造函数的作用:实例化类的时候自动调用,执行每个方法前都会自动执行一次
public function __construct()
{
$this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
/**
* 判断索引是否存在
* @param string $index_name
* @return bool|mixed|string
*/
public function exists_index($index_name = 'test_ik')
{
$params = [
'index' => $index_name
];
try {
return $this->client->indices()->exists($params);
} catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
/**
* 创建索引
* @param string $index_name
* @return array|mixed|string
*/
public function create_index($index_name = 'test_ik') { // 只能创建一次
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 1
]
]
];
try {
return $this->client->indices()->create($params);
} catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
/**
* 删除索引
* @param string $index_name
* @return array
*/
public function delete_index($index_name = 'test_ik') {
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
/**
* 添加文档
* @param $id
* @param $doc ['id'=>100, 'title'=>'phone']
* @param string $index_name
* @param string $type_name
* @return array
*/
public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $doc
];
$response = $this->client->index($params);
return $response;
}
/**
* 判断文档存在
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array|bool
*/
public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->exists($params);
return $response;
}
/**
* 获取文档
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array
*/
public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->get($params);
return $response;
}
/**
* 更新文档
* @param int $id
* @param string $index_name
* @param string $type_name
* @param array $body ['doc' => ['title' => '苹果手机iPhoneX']]
* @return array
*/
public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) {
// 可以灵活添加新字段,最好不要乱添加
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $body
];
$response = $this->client->update($params);
return $response;
}
/**
* 删除文档
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array
*/
public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->delete($params);
return $response;
}
/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse 搜索
*/
public function esSearch($data)
{
//获取搜索的数据
$word = $data;
if (empty($word)) {
// $data = Housing::with(['HouOtt', 'HouPay', 'HouSet', 'HouType'])->get()->toArray();
return response()->json(['code' => 1, 'msg' => 'word参数不能为空', 'data' => $word]);
}
//分页
// $page = $request->get('page', 1);//接收当前页,默认值是1
// $size = config('pagesize');//每页显示条数
// $from = ($page - 1) * $size;//偏移量
$params = [
'index' => 'goods', //自己的索引名称
'body' => [
//执行
'query' => [
//匹配
'match' => [
'title' => $word
]
],
'highlight' => [
'pre_tags' => ["<span style='color: red'>"],
'post_tags' => ['</span>'],
'fields' => [
'title' => new \stdClass()
]
]
],
// 'size' => $size,//每页显示的条数
// 'from' => $from//偏移量
];
$res = $this->client->search($params);
$data = $res['hits']['hits'];
foreach ($data as $k => $v) {
$data[$k]['_source']['title'] = $v['highlight']['title'][0];
}
$data = array_column($data, '_source');
return $data;
}
}
//调用方式(其中一种)
$es = new MyEs();
$data = $es->方法(); //注意是否需要传参
//视图高亮显示
<td>{!! $v['title'] !!}</td>