全文检索示例:前端页面编写(10)

简介

本文在上一章节springboot集成es基础上完成全文检索接口编写
完整代码github地址

添加maven依赖

前端页面使用thymeleaf模板,为了修改页面不重启服务添加devtool依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
 </dependency>

禁用thymeleaf缓存

application.yml中添加如下配置

spring:
  thymeleaf:
    cache: false

试图跳转Controller

编写ViewController,用于跳转视图,当访问地址http://localhost:8080/search/index时跳转到resources/templates/search/index.html视图

package app.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author faith.huan 2019-10-22 21:25
 */
@Controller
public class ViewController {

    @RequestMapping("search/index")
    public String searchIndex(){
        return "search/index";
    }

}

页面编写

完整代码如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="">

<head>
    <title>Elastic search 文档</title>

    <meta charset="UTF-8">
    <!-- 引入element样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 先引入 Vue -->
    <script type="text/javascript" src="https://unpkg.com/vue"></script>
    <!-- 引入element组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>

    <style>
        /* 查询类型选择框宽度和背景色设置 */
        .el-select .el-input {
            width: 150px;
        }

        .input-with-select .el-input-group__prepend {
            background-color: #fff;
        }

        /*标题和内容的字体等设置*/
        .title {
            font-size: 24px;
            cursor: pointer;
        }

        .content {
            margin-bottom: 4px;
            color: #8a8a8a;
            font-size: 14px;
            line-height: 24px;
        }

    </style>
</head>

<body>
<div id="app">

    <!-- 查询框部分 -->
    <el-row>
        <el-col :span="12" :offset="6">
            <el-input placeholder="请输入内容" v-model="keyword" class="input-with-select" @keyup.enter.native="search">
                <el-select v-model="type" slot="prepend" placeholder="请选择" @change="typeChange">
                    <el-option label="单词检索" value="term"></el-option>
                    <el-option label="短语检索" value="phrase"></el-option>
                </el-select>
                <el-button slot="append" type="primary" @click.native.prevent="search"
                           icon="el-icon-search">搜索
                </el-button>
            </el-input>
        </el-col>
    </el-row>

    <!-- 内容显示部分 -->
    <el-row>
        <el-col :span="16" :offset="4" v-for="(content, index) in contents" :key="content.oid">
            <el-card :body-style="{ padding: '0px' }">
                <div class="clearfix" style="padding: 14px;">
                    <div>
                        <span class="title" v-html="content.title" @click="view(content.url)"></span>
                        <span style="float: right">评分:{{content.score}}</span>
                    </div>
                    <span class="content" v-html="content.content"></span>
                    <div class="bottom clearfix">
                        <i style="float: right" class="el-icon-time">{{ content.toEsDate }}</i>
                    </div>
                </div>
            </el-card>
        </el-col>
    </el-row>

    <!-- 分页插件部分 -->
    <el-row>
        <el-col :span="16" :offset="4">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 20, 50, 100]"
                    :page-size="pageSize"
                    layout="sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-col>
    </el-row>


</div>
</body>


<script>
    var vm = new Vue({
        el: '#app',
        data: {
            currentPage: 1, // 初始页码
            pageSize: 10,   // 每页的数据
            total: 0,       // 总记录数
            keyword: '权威指南', // 关键词
            type: 'term',       //查询方式
            contents: []        //查询结果
        },
        created: function () {
            this.search()
        },
        methods: {
            // 每页大小变更处理函数
            handleSizeChange: function (size) {
                this.pageSize = size;
                console.log("每页大小:" + this.pageSize);  //每页下拉显示数据
                this.search();
            },
            // 页码变更处理函数
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
                console.log("当前页码:" + this.currentPage);  //点击第几页
                this.search();
            },
            search: function () {
                // 模糊弹层
                var loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                // 查询参数
                var data = {
                    keyword: this.keyword,
                    type: this.type,
                    page: this.currentPage - 1,
                    pageSize: this.pageSize
                };
                // post请求
                $.post("/search/fullTextSearch", data)
                    .then(function (response) {
                        console.log("response", response);
                        if (response && response.totalElements && response.content) {
                            console.log("总条数:" + response.totalElements);
                            vm.total = response.totalElements;
                            vm.contents = response.content;
                        } else {
                            vm.total = 0;
                            vm.contents = [];
                        }
                        loading.close();
                    });
            },
            // 点击title时,跳转官方地址
            view: function (url) {
                console.log(url);
                window.open(url, "_blank");
            },
            // 查询类型变更时,当前页码改为1
            typeChange: function () {
                this.currentPage = 1;
            }
        }
    })
</script>

</html>


测试截图

term查询

短语查询能查询到三个文档,与上一章节接口测试一致
在这里插入图片描述

phrase查询

短语查询能查询到一个文档,与上一章节接口测试一致

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例: HTML代码: ```html <!DOCTYPE html> <html> <head> <title>AJAX Table Example</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="script.js"></script> </head> <body> <h1>Employee List</h1> <table id="employeeTable"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <tbody> </tbody> </table> </body> </html> ``` JavaScript代码(script.js): ```javascript $(document).ready(function() { getEmployeeList(); // on page load, get the employee list }); function getEmployeeList() { $.ajax({ url: "getEmployeeList.php", // URL of the server-side script to handle the AJAX request method: "GET", dataType: "json", success: function(response) { // on success, populate the table with data var employeeTable = $("#employeeTable tbody"); employeeTable.empty(); // clear any existing rows in the table for (var i = 0; i < response.length; i++) { var row = "<tr>"; row += "<td>" + response[i].id + "</td>"; row += "<td>" + response[i].name + "</td>"; row += "<td>" + response[i].email + "</td>"; row += "<td>" + response[i].phone + "</td>"; row += "</tr>"; employeeTable.append(row); // add the new row to the table } }, error: function(xhr, status, error) { // on error, display an error message alert("Error: " + status + " - " + error); } }); } ``` 以上代码将从服务器获取JSON格式的员工列表数据,并将其显示到HTML表格中。在服务器端,您需要编写一个处理AJAX请求的脚本(在此示例中为`getEmployeeList.php`),该脚本将从数据库中检索数据并将其以JSON格式返回给客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值