[关闭]
@ruoli 2017-03-20T13:46:41.000000Z 字数 3070 阅读 1841

Javascript jquery 请求 ElasticSearch

Web前端


  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  8. <title>搜索引擎示例页面</title>
  9. <!-- Bootstrap -->
  10. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  11. <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  12. <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  13. <!--[if lt IE 9]>
  14. <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  15. <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
  16. <![endif]-->
  17. <style type="text/css">
  18. .container{
  19. padding-top: 50px;
  20. }
  21. .input-group-addon{
  22. cursor: pointer;
  23. }
  24. .input-group-addon:hover{
  25. background-color: #3A5C8B;
  26. color: #fff;
  27. }
  28. #search-result{
  29. margin: 50px 0;
  30. display: none;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <h2>搜索示例</h2>
  37. <div class="input-group">
  38. <input type="text" class="form-control" id="search-content">
  39. <span class="input-group-addon" id="search-btn">搜索</span>
  40. </div>
  41. <h5>本示例采用ElasticSearch作为搜索引擎,所有数据均来源于搜索引擎</h5>
  42. <div class="panel panel-default" id="search-result">
  43. <div class="panel-heading">搜索结果</div>
  44. <div class="panel-body" id="result-content">
  45. <table class="table table-striped table-bordered table-hover">
  46. <thead>
  47. <tr>
  48. <th>序号</th>
  49. <th>时间</th>
  50. <th>设备名称</th>
  51. <th>厂站</th>
  52. <th>电压等级</th>
  53. <th>动作类型</th>
  54. <th>区域</th>
  55. <th>动作次数</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. </tbody>
  60. </table>
  61. <p>总共搜索到<span id="total"></span>条结果,耗时<span id="took"></span></p>
  62. </div>
  63. </div>
  64. </div>
  65. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  66. <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
  67. <!-- Include all compiled plugins (below), or include individual files as needed -->
  68. <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  69. <script type="text/javascript">
  70. $(function(){
  71. $("#search-btn").click(function(){
  72. var searchContent=$("#search-content").val();
  73. if(searchContent!=''){
  74. var data= JSON.stringify({"query": {"multi_match": {"query": searchContent,"fields":["sbmc","cz","dydj","dzlx","qy","sj"]}}});
  75. jQuery.ajax({
  76. url: "http://10.122.111.83:9200/library/tj_bhdz/_search",
  77. type: "POST",
  78. data: data,
  79. dataType: "json",
  80. success: function(msg) {
  81. $("#total").text(msg.hits.total);
  82. $("#took").text(msg.took/1000);
  83. loadData(msg.hits.hits);
  84. },
  85. error: function(XMLHttpRequest, textStatus, errorThrown) {
  86. alert("发生错误");
  87. // alert(JSON.stringify(XMLHttpRequest));
  88. // alert(XMLHttpRequest.readyState);
  89. // alert(textStatus);
  90. },
  91. complete: function(XMLHttpRequest, textStatus) {
  92. this; // 调用本次AJAX请求时传递的options参数
  93. }
  94. });
  95. }
  96. });
  97. });
  98. function loadData(data){
  99. $(".table").find("tbody").html("");
  100. $(data).each(function(i,n){
  101. var td="<tr><td>"+(i+1)+"</td>"+
  102. "<td>"+n._source.sj+"</td>"+
  103. "<td>"+n._source.sbmc+"</td>"+
  104. "<td>"+n._source.cz+"</td>"+
  105. "<td>"+n._source.dydj+"</td>"+
  106. "<td>"+n._source.dzlx+"</td>"+
  107. "<td>"+n._source.qy+"</td>"+
  108. "<td>"+n._source.dzcs+"</td></tr>";
  109. $(".table").find("tbody").append(td);
  110. });
  111. $("#search-result").fadeIn(200);
  112. }
  113. $('#search-content').keydown(function(e){
  114. if(e.keyCode==13){
  115. $("#search-btn").click();
  116. }
  117. });
  118. </script>
  119. </body>
  120. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注