1. 检查电脑有没有Java SE环境(CMD中输入java -version),如果没有的话,没有的话请移步Java Archive | Oracle 下载JAVA SE,下载完成之后配置环境变量。不会配环境的移步
2.下载ES与Kibana 移步 下载 Elastic 产品 | Elastic
3.下载ik分词器 移步 https://github.com/medcl/elasticsearch-analysis-ik/releases
4.ES KIBANA IK 分词器下载的版本要一致 如 ES使用7.1.0 kibana也要7.1.0 ik分词也要7.1.0
5.下载完IK分词器后,解压到这个目录下并将文件夹名改为ik
6.启动ES 与 KIBANA
启动ES
启动KIBANA
启动完之后,到浏览器检查是否启动成功 ES:(http://localhost:9200)KIBANA(http://localhost:5601),例 ES启动成功
例 KIBANA启动成功:
7. 框架内执行composer require elasticsearch/elasticsearch 安装扩展。
各种准备工作结束,接下来上代码
1. 创建索引
public function createIndex(){
// 创建索引
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'news',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
// ‘title’ 字段
'title' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
],
'author' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
return $response;
}
2. 第二步,将数据同步到ES (这里看你的逻辑是什么)
//封装一个同步的方法,到时候直接调用该方法
public function pushEsNews($data){
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'news',
'type' => '_doc',
'body' => $data
];
$response = $client->index($params);
return $response;
}
//我这边用的采集,你可以换成正常的添加数据。
public function queryList(){
//数据采集
$ql = QueryList::get("http://www.rz-tea.com/");
$data['author'] = $ql->find('.topic a')->texts();
$data['title'] = $ql->find('.article-title a')->texts();
$data['src'] = $ql->find('.article-image img')->attrs('src');
$data['issue_date'] = $ql->find('.article-meta .publish')->texts();
$data['intro'] = $ql->find('.article-digest')->texts();
$data['info_src'] = $ql->find('.article-image a')->attrs('href');
foreach ($data['author'] as $key=>$val){
unset($newData);
$newData['author'] = $val;
$newData['title'] = $data['title'][$key];
$newData['src'] = $data['src'][$key];
$newData['issue_date'] = $data['issue_date'][$key];
$newData['info_src'] = $data['info_src'][$key];
$newData['intro'] = $data['intro'][$key];
$id = News::insertGetId($newData);
//将数据同步到ES
$newData['id'] = $id;
$this->pushEsNews($newData);
}
}
3.搜索后高亮显示
public function show(request $request){
//搜索的值
$seach = $request['seach'];
$res = News::get()->toArray();
//判断用户是否搜索,如果没有则跳过
if($seach!=""){
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'news',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'match' => [
'title' => "$seach",
]
]
]
]
],
'highlight' => [
'pre_tags' => ["<font color='red'>"],
'post_tags' => ["</font>"],
'fields' => [
"title" => new \stdClass()
]
]
]
];
$response = $client->search($params);
$data = $response['hits']['hits'];
$res = [];
foreach ($data as $v) {
if (!empty($v['highlight']['title'][0])) {
$v['_source']['title'] = $v['highlight']['title'][0];
}
array_push($res, $v['_source']);
}
}
return view('news_show',compact('res','seach'));
}
4.前台展示HTML
<div class="page-container">
<form action="" method="get">
<div class="text-c">
<input type="text" class="input-text" style="width:250px" placeholder="请输入新闻标题" id="" name="seach" value="{{$seach}}">
<button type="submit" class="btn btn-success" id="" name=""><i class="Hui-iconfont"></i> 搜新闻</button>
</div>
</form>
<div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l"><a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 批量删除</a> <a href="javascript:;" onclick="admin_add('添加管理员','admin-add.html','800','500')" class="btn btn-primary radius"><i class="Hui-iconfont"></i> 添加管理员</a></span> <span class="r">共有数据:<strong>54</strong> 条</span> </div>
<table class="table table-border table-bordered table-bg">
<thead>
<tr>
<th scope="col" colspan="9">员工列表</th>
</tr>
<tr class="text-c">
<th width="15"><input type="checkbox" name="" value=""></th>
<th width="20">ID</th>
<th width="30">发布作者</th>
<th width="100">新闻标题</th>
<th width="200">新闻简介</th>
<th width="130">新闻封面</th>
<th width="20">发布时间</th>
<th width="20">是否启用</th>
<th width="10">操作</th>
</tr>
</thead>
<tbody>
@foreach($res as $key=>$val)
<tr class="text-c">
<td><input type="checkbox" value="{{$val['id']}}" name=""></td>
<td>{{$val['id']}}</td>
<td>{{$val['author']}}</td>
<td>{!!$val['title']!!}</td>
<td>{{$val['intro']}}</td>
<td><img src="{{$val['src']}}" alt="" width="100%" height="100%"></td>
<td>{{$val['issue_date']}}</td>
<td class="td-status"><span class="label label-success radius">已启用</span></td>
<td class="td-manage"><a style="text-decoration:none" onClick="admin_stop(this,'10001')" href="javascript:;" title="停用"><i class="Hui-iconfont"></i></a> <a title="编辑" href="javascript:;" onclick="admin_edit('管理员编辑','admin-add.html','1','800','500')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a> <a title="删除" href="javascript:;" onclick="admin_del(this,'1')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
最终效果:
结束!