使用ES+JavaWeb实现关键词索引相关文章内容

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ES+JavaWeb实现关键词索引相关文章内容相关的知识,希望对你有一定的参考价值。

ElasticSearch + JavaWeb

使用JavaWeb提供一个页面和搜索框,在搜索框中输入关键词,通过ES检索,找出匹配到的文章内容

一 JavaCode

1.ES2Web

package com.zxy.web.servlet;

import com.zxy.pojo.Suitable;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class ES2Web {
    public static List<String> getIndexWord(String indexWord) throws UnknownHostException {
        System.out.println("ES2Web.getIndexWord():" + indexWord);
        List<String> ulist = new ArrayList<String>();
        Suitable suitable = new Suitable();
        Settings settings = Settings.builder()
                .put("cluster.name", "hzbigdata2101")
                .build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);

        client.addTransportAddresses(
                new TransportAddress(InetAddress.getByName("192.168.130.110"), 9300)
        );
        SearchResponse searchResponse = client.prepareSearch("chinese")
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.matchQuery("content", indexWord))
                .get();

        SearchHits hits = searchResponse.getHits();
        System.out.println("totalHits:"+hits.totalHits);
        System.out.println("maxSource:"+hits.getMaxScore());
        SearchHit[] searchHits = hits.getHits();
        for(SearchHit hit:searchHits){
            String[] split = hit.getSourceAsString().split(":");
            System.out.println(split[0] + " + " + split[1]);
            int i = split[1].lastIndexOf("\\"");
            System.out.println("i" + i);
            String substring = split[1].substring(1, i);
            System.out.println(substring);
            ulist.add(substring);


            suitable.setSuitableWord(ulist);

            System.out.println("index:"+hit.getIndex());
            System.out.println("type:"+hit.getType());
            System.out.println("docID:"+hit.getId());
            System.out.println("content:"+hit.getSourceAsString());
        }
        return suitable.getSuitableWord();
    }
}

2.LoginServlet

package com.zxy.web.servlet;

import com.zxy.service.UserService;
import com.zxy.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String indexWord1 = request.getParameter("indexWord");
        String indexWord = new String(indexWord1.getBytes("ISO-8859-1"),"utf-8");
        System.out.println("loginservlet->indexword:"+indexWord);
        List<String> suitable = ES2Web.getIndexWord(indexWord);
        System.out.println("匹配到的数据:" + suitable);
        if (suitable != null) {
            request.getSession().setAttribute("indexWord", indexWord);
            request.getSession().setAttribute("suitable", suitable);
            response.sendRedirect("/success.jsp");
        }else {
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }
}

二 JspCode

1. index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
    <head>
        <title>登陆</title>
    </head>
    <body>
        <%-- 所有的表单提交都使用post方式提交 --%>
        <%-- 项目名/servlet路径:
            绝对路径:/loginServlet
            相对路径:loginServlet
        --%>
        <form action="/loginServlet" method="post">
            关键字:<input type="text" name="indexWord" ><br>
<%--            关键字:<input name="indexWord" onkeyup="value=value.replace(/[^\\w\\u4E00-\\u9FA5]/g, '')"/><br>--%>
            <input type="submit" value="搜索">
        </form>
    </body>
</html>

2.success.jsp

<%@ page import="com.zxy.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.zxy.pojo.Suitable" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>successful</title>
</head>
<%--
jsp 有9大内置对象
page
pageContext
request
response
session
application
exception
out
--%>
<body>
<h2>搜索引擎</h2>
    <%
        Object indexWord = session.getAttribute("indexWord");
        System.out.println("success.jsp->indexword:" + indexWord);
    %>

    <h3>关键词:<%=indexWord%></h3>
    <h3>匹配项:</h3>
    <%
        List<String> suitables = (List<String>) session.getAttribute("suitable");
        for(String suitable:suitables){
    %>
        <h4><%=suitable%></h4>
    <%
        }
    %>
</body>
</html>

3.error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>error</title>
</head>
<body>
    <h1 style="color: red">你的用户名或者密码有误!</h1>
</body>
</html>

三 ES导入数据

curl -HContent-Type:application/json -XPUT 'http://hadoop:9200/chinese/test/3?pretty' -d \\
'
{
    "content":"运动员还在国际舞台当中取得冠军"
}
'
curl -HContent-Type:application/json -XPUT 'http://hadoop:9200/chinese/test/4?pretty' -d \\
'
{
    "content":"运动员,指从事体育运动的人员,词语起源于古希腊文。运动员分为运动健将、一级运动员、二级运动员、三级运动员、少年级运动员五个技术等级。"
}
'
curl -HContent-Type:application/json -XPUT 'http://hadoop:9200/chinese/test/7?pretty' -d \\
'
{
    "content":"SSM框架简要介绍_Mr.zhou_Zxy-CSDN博客_简要介绍ssm框架"
}
'
等数据....

使用ES+JavaWeb实现关键词索引相关文章内容

以上是关于使用ES+JavaWeb实现关键词索引相关文章内容的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch 字段类型

ElasticSearch 实现分词全文检索

elasticSearch - 原理

PHP简单实现“相关文章推荐”功能的方法

22.es各操作实现原理

运维工程师监控工作之Elasticsearch关键指标采集方法