@ruoli
2017-03-20T13:46:41.000000Z
字数 3070
阅读 1841
Web前端
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>搜索引擎示例页面</title>
<!-- Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.container{
padding-top: 50px;
}
.input-group-addon{
cursor: pointer;
}
.input-group-addon:hover{
background-color: #3A5C8B;
color: #fff;
}
#search-result{
margin: 50px 0;
display: none;
}
</style>
</head>
<body>
<div class="container">
<h2>搜索示例</h2>
<div class="input-group">
<input type="text" class="form-control" id="search-content">
<span class="input-group-addon" id="search-btn">搜索</span>
</div>
<h5>本示例采用ElasticSearch作为搜索引擎,所有数据均来源于搜索引擎</h5>
<div class="panel panel-default" id="search-result">
<div class="panel-heading">搜索结果</div>
<div class="panel-body" id="result-content">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>序号</th>
<th>时间</th>
<th>设备名称</th>
<th>厂站</th>
<th>电压等级</th>
<th>动作类型</th>
<th>区域</th>
<th>动作次数</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p>总共搜索到<span id="total"></span>条结果,耗时<span id="took"></span> 秒 </p>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function(){
$("#search-btn").click(function(){
var searchContent=$("#search-content").val();
if(searchContent!=''){
var data= JSON.stringify({"query": {"multi_match": {"query": searchContent,"fields":["sbmc","cz","dydj","dzlx","qy","sj"]}}});
jQuery.ajax({
url: "http://10.122.111.83:9200/library/tj_bhdz/_search",
type: "POST",
data: data,
dataType: "json",
success: function(msg) {
$("#total").text(msg.hits.total);
$("#took").text(msg.took/1000);
loadData(msg.hits.hits);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("发生错误");
// alert(JSON.stringify(XMLHttpRequest));
// alert(XMLHttpRequest.readyState);
// alert(textStatus);
},
complete: function(XMLHttpRequest, textStatus) {
this; // 调用本次AJAX请求时传递的options参数
}
});
}
});
});
function loadData(data){
$(".table").find("tbody").html("");
$(data).each(function(i,n){
var td="<tr><td>"+(i+1)+"</td>"+
"<td>"+n._source.sj+"</td>"+
"<td>"+n._source.sbmc+"</td>"+
"<td>"+n._source.cz+"</td>"+
"<td>"+n._source.dydj+"</td>"+
"<td>"+n._source.dzlx+"</td>"+
"<td>"+n._source.qy+"</td>"+
"<td>"+n._source.dzcs+"</td></tr>";
$(".table").find("tbody").append(td);
});
$("#search-result").fadeIn(200);
}
$('#search-content').keydown(function(e){
if(e.keyCode==13){
$("#search-btn").click();
}
});
</script>
</body>
</html>