使用ElasticSearch实现搜索时即时提示与全文搜索功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#header_search_suggest{
position: absolute;
width: calc(100% - 10px);
left: 4px;
border: solid 1px #ccc;
background-color: white;
text-align: left;
z-index: 101;
display: block;
}
#header_search_suggest li{
font-size: 14px;
border-bottom: 1px solid #eeeeee;
}
#header_search_suggest li a{
padding:0.5em 1em;
color:#333333;
display: block;
text-decoration: none;
}
#header_search_suggest li a:hover{
background-color: #EDF0F2;
color:#2F7EC4;
}
#header_search_suggest li a em{
font-style: italic;
color:#999;
font-size:0.8em;
}
.btn{width: 7em;} </style> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script> </head>
<body> <div class="container" style="margin-top: 3em;"> <form method="post" action="/index.php/index/index/getsearch" id="header_search" class="form-inline">
<div class="form-group">
<label for="exampleInputName2">输入搜索关键词试试</label>
<input type="text" class="form-control" id="keyword" name="keyword" value="" autocomplete="off" />
</div>
<div class="form-group">
<input type="submit" value="搜索" class="btn btn-success"/>
</div> </form>
<ul id="header_search_suggest"></ul> </div> <!-- js部分,这部分控制,输入框输入时,进行及时提示的功能 -->
<script>
var xhr = null;
$('#keyword').bind('input propertychange', function () {
if (xhr) {
xhr.abort();//如果存在ajax的请求,就放弃请求
}
var inputText = $.trim(this.value);
if (inputText != "") { //检测键盘输入的内容是否为空,为空就不发出请求
xhr = $.ajax({
type: 'POST',
url: '/index.php/index/index/search',//注意这里输入框输入进行及时提示的方法与action方法不同
cache: false,//不从浏览器缓存中加载请求信息
// data: "keyword=" + inputText,
data: {keyword: inputText},
dataType: 'json',
success: function (json) {
//console.log(json); json是返回的json数据
if (json.count != 0) {
//检测返回的结果是否为空
var lists = "";
// console.log(json[0]._source.title);
//由于返回多条数据,进行each遍历
$.each(json, function (index, obj) {
//返回多条数据,index是第几条,obj是内容
//处理高亮关键词(这里是输入关键词自动出现的列表的样式)
console.log(json[index]._source.title);
var searchContent = json[index]._source.title;//标题
var suggestItem = '';
if (searchContent.toLowerCase().indexOf(inputText.toLowerCase()) > -1) {
var searchRegExp = new RegExp('(' + inputText + ')', "gi");
suggestItem = searchContent.replace(searchRegExp, ("<strong>$1</strong>"));
}
suggestItem = suggestItem + "<em> - " + json[index]._type + ' * ' + json[index]._id + "</em>";
//遍历出每一条返回的数据
lists += "<li class='listName' ><a href='/index.php/index/index/search?id=" + json[index]._id + "&key=" + encodeURI(searchContent + ' - ' + json[index]._type) + "'>" + suggestItem + "</a></li>";
}); $("#header_search_suggest").html(lists).show();//将搜索到的结果展示出来
} else {
$("#header_search_suggest").hide();
}
//记录搜索历史记录
$.post('/index.php/index/index/savesearchlog',{keyword: inputText,count: json.count});
}
});
} else {
$("#header_search_suggest").hide();//没有查询结果就隐藏搜索框
}
}).blur(function () {
setTimeout('$("#header_search_suggest").hide()',500);//输入框失去焦点的时候就隐藏搜索框,为了防止隐藏过快无法点击,设置延迟0.5秒隐藏
});
</script> </body>
</html>
以上是html部分,下面是php部分
public function getsearch(){
//这个方法是表单点击搜索时action提交的方法
$client = ClientBuilder::create()->build();
$keys = Request::instance()->param('keyword');
$keys = $keys ? $keys : '测试';
$params = [
'index' => 'article_index',
'type' => 'article_type',
'body' => [
'query' => [
'match' => [
'content' => $keys
]
]
]
];
$response = $client->search($params);
$str = '';
$list = $response['hits']['hits'];
//pp($list);die;
$str .= '<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>'; $str .= '<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>content</th>
</tr>
</thead>
<tbody>'; foreach ($list as $k => $v) {
$str .= '<tr><td>' . $v['_source']['id'] . '</td><td>' . $v['_source']['title'] . '</td><td>' . $v['_source']['content'] . '</td></tr>';
}
$str .='</tbody></table>';
return $str; } public function search() {
//这部分方法是ajax 在搜索框输入文字时进行提示的方法
/*$client = ClientBuilder::create()->setHosts($hosts)->build();*/
//实例化es类;在项目中引入自动加载文件,并且实例化一个客户端:
$client = ClientBuilder::create()->build(); $keys = Request::instance()->param('keyword');//tp5方法,获取get post数据自动辨别
$keys = $keys ? $keys : '6';
$params = [
'index' => 'article_index',
'type' => 'article_type',
'body' => [
'query' => [
'match' => [
'content' => $keys
]
]
]
];
$response = $client->search($params);
return json($response['hits']['hits']);
//pp($response['hits']['hits']);
die;
}
最终效果
使用ElasticSearch实现搜索时即时提示与全文搜索功能的更多相关文章
- 使用ElasticSearch服务从MySQL同步数据实现搜索即时提示与全文搜索功能
最近用了几天时间为公司项目集成了全文搜索引擎,项目初步目标是用于搜索框的即时提示.数据需要从MySQL中同步过来,因为数据不小,因此需要考虑初次同步后进行持续的增量同步.这里用到的开源服务就是Elas ...
- ElasticSearch 2 (14) - 深入搜索系列之全文搜索
ElasticSearch 2 (14) - 深入搜索系列之全文搜索 摘要 在看过结构化搜索之后,我们看看怎样在全文字段中查找相关度最高的文档. 全文搜索两个最重要的方面是: 相关(relevance ...
- MySQL全文搜索
http://www.yiibai.com/mysql/full-text-search.html 在本节中,您将学习如何使用MySQL全文搜索功能. MySQL全文搜索提供了一种实现各种高级搜索技术 ...
- VuePress 博客优化之开启 Algolia 全文搜索
前言 在 <一篇带你用 VuePress + Github Pages 搭建博客>中,我们使用 VuePress 搭建了一个博客,最终的效果查看:TypeScript 中文文档. 由于 V ...
- jquery+php实现用户输入搜索内容时自动提示
index.html <html> <head> <meta charset=;} #search_auto li a:hover{background:#D8D ...
- Elasticsearch是一个分布式可扩展的实时搜索和分析引擎,elasticsearch安装配置及中文分词
http://fuxiaopang.gitbooks.io/learnelasticsearch/content/ (中文) 在Elasticsearch中,文档术语一种类型(type),各种各样的 ...
- Ternary Search Tree 应用--搜索框智能提示
前面介绍了Ternary Search Tree和它的实现,那么可以用Ternary Search Tree来实现搜索框的只能提示,因为Ternary Search Tree的前缀匹配效率是非常高的, ...
- 发掘ListBox的潜力(三):显示即时提示(Tips)
ListBox显示即时提示(Tips) Listbox内容太长时超出Listbox宽度的部分将无法显示,一种解决方法是让Listbox产生横向滚动条,滚动显示内容(见前面的<发掘ListBox的 ...
- 全文搜索之 Elasticsearch
概述 Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的. 特性 安装方便:没有其他依赖,下载 ...
随机推荐
- set get del
//设置 $ob = new Redis(); $ob->connect('127.0.0.1', 6379); $re = $ob->set('str1', serialize(['a' ...
- c#实战开发:用.net core开发一个简单的Web以太坊钱包 (六)
今天就来开发一个C# 版的简易钱包 先回顾以前的内容 c#实战开发:以太坊Geth 命令发布智能合约 (五) c#实战开发:以太坊Geth 常用命令 (四) c#实战开发:以太坊钱包快速同步区块和钱包 ...
- Repeater 控件的嵌套使用
Repeater 控件的嵌套使用 ItemDataBound:数据绑定的时候(正在进行时)发生,多用在Repeater控件嵌套,对子Repeater控件进行数据绑定及模板列中统计列的计算处理等 ...
- (5)Jquery1.8.3快速入门_层次选择器
一.Jquery的选择器: 层级选择器: 1.空格 div span div中的包含的所有span后代元素 2. > ...
- Strust2框架笔记01_XML配置_action编写
目录 1.Struts2概述 1.1 什么是Struts2 1.2 Web层框架基于前端控制器模型设计 2. Struts2入门案例 2.1 Struts2的开发环境 2.2 解压开发包 2.3 创建 ...
- 【Java基础】9、Enumeration接口和Iterator接口的区别
package java.util; public interface Enumeration<E> { boolean hasMoreElements(); E next ...
- Redis的五种数据类型的简单介绍和使用
1.准备工作: 1.1在Linux下安装Redis https://www.cnblogs.com/dddyyy/p/9763098.html 1.2启动Redis 先把root/redis的red ...
- wepy里面两种不同的写回调函数的方法
方案一const getHelpCenter = createAction(GET_HELP_CENTER, () => request('api/hisense/article/menu/li ...
- Linux 学习笔记之超详细基础linux命令 Part 7
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...
- U8 应付款管理 单据类型 分析
Ap_CloseBill 收付款单主表 Ap_CloseBills 收付款单子表 cVouchType 在收付款单主表中 ,用于区分单据为收款单还是付款单(48,49) 49:付款单 48:收 ...