composer安装命令
composer require elasticsearch/elasticsearch
在app目录下创建ES目录及MyEs类
client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
public function exists_index($index_name = 'test_ik')
{
$params = [
'index' => $index_name
];
try {
return $this->client->indices()->exists($params);
} catch (ElasticsearchCommonExceptionsBadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
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 (ElasticsearchCommonExceptionsBadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
public function delete_index($index_name = 'test_ik') {
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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' => [""],
'post_tags' => [''],
'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->方法(); //注意是否需要传参
//视图高亮显示
{!! $v['title'] !!}



