当前位置:   article > 正文

Java文档搜索引擎总结_搜索引擎项目总结

搜索引擎项目总结

项目介绍

Java文档搜索引擎项目是一个SSM项目,该项目的前端界面部分是由搜索页面和展示页面组成,后端部分索引模块(ScanAnalysis、index)、搜索模块(Searcher)、Web模块(SearcherController)。该项使用ansj第三方分词库进行分词,该项目并没有使用爬虫程序来获取Java文档,而是直接将Java文档下载下来,将Java文档里面的内容进行分词保存到正排索引文件和倒排索引文件中。

项目使用的技术栈

HTML、CSS、JS、Ajax、SpringBoot、SpringMVC

前端页面展示

搜索页面:
在这里插入图片描述
显示页面:
在这里插入图片描述

后端逻辑部分

索引部分

索引部分底层实现了两个类:ScanAnalysis类、Index类
***ScanAnalysis类:***用来扫描Java文档中的所有HTML文件,将HTML文件的标题、url路径、正文保存到正排索引文件和倒排索引文件中。
***Index类:***底层实现了正排索引结构和倒排索引结构,Index类是配合ScanAnalysis类一起使用的,Index将HTML文件内容保存到正排索引和倒排索引结构中,最终保存到正排索引文件和倒排索引文件中。

ScanAnalysis类的底层代码:

public class ScanAnalysis {
   

    //要扫描的根路径
    private static final String PATH_ROOT = "D:\\知识复习思维导图(Java)和Java笔记\\project-warehouse\\jdk-8u351-docs-all\\docs\\api";

    //Java文档的网络地址 不同部分
    private static final String JAVA_PATN = "https://docs.oracle.com/javase/8/docs/api/";

    //索引对象
    private static Index index = new Index();
    /**
     * 启动方法
     * 我们在进行扫描的时候,我们会发现在进行扫描的时候效率是比较低的。
     * 该方法使用的是单线程的方式
     * 我们可以使用多线程的方式来提高效率
     */
    public void run() {
   
        long ben1 = System.currentTimeMillis();
        //保存每一个文档的路径
        ArrayList<String> arrayList = new ArrayList<>();
        //1.获取每一个文档的路径
        scanPath(PATH_ROOT,arrayList);
        long ben = System.currentTimeMillis();
        //2.对每一个html文件进行解析
        for (String pathChild:arrayList) {
   
            analysis(pathChild);
        }
        long end = System.currentTimeMillis();
        System.out.println("解析所花费的时间:"+(end - ben)+"ms");
        //3.将索引保存的索引文档中
        index.saveFile();
        long end1 = System.currentTimeMillis();

        System.out.println("整个程序的时间:"+(end1 - ben1) +"ms");
    }

    /**
     * 启动方法2:我们对解析这个步骤使用多线程的方式来提高效率
     *
     */
    public void run2() {
   
        long ben1 = System.currentTimeMillis();
        //保存每一个文档的路径
        ArrayList<String> arrayList = new ArrayList<>();
        //1.获取每一个文档的路径
        scanPath(PATH_ROOT,arrayList);
        long ben = System.currentTimeMillis();
        //2.对每一个html文件进行解析
        //我们创建一个有时光线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(15);
        //这个CountDownLatch对象,是用来表明需要等待多少个任务才结束
        //因为我们要等到解析这个过程完成了在执行下一步
        CountDownLatch countDownLatch = new CountDownLatch(arrayList.size());
        for (String pathChild:arrayList) {
   
            //将解析的工作提交倒线程池中
            executorService.submit(new Runnable() {
   
                @Override
                public void run() {
   
                    analysis(pathChild);
                    //完成一次解析任务就减一
                    countDownLatch.countDown();
                }
            });
        }


        try {
   
            //等待任务结束,如果没结束,就阻塞等待
            countDownLatch.await();
            //关闭线程池
            executorService.shutdown();
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("解析所花费的时间:"+(end - ben)+"ms");
        //3.将索引保存的索引文档中
        index.saveFile();
        long end1 = System.currentTimeMillis();

        System.out.println("整个程序的时间:"+(end1 - ben1) +"ms");
    }

    /**
     * 对 HTML文件进行解析
     * 获取到题目、正文、url
     * @param pathChild
     */
    private void analysis(String pathChild) {
   
        File file = new File(pathChild);
        //1.获取标题
        String title = getTitle(file);
//        System.out.println(title);
        //2.获取正文
        String content = getContents(file);
        //3.获取url
        String url = getUrl(file);
        System.out.println(url);
        //4.将标题、正文、url保存到索引中
        index.saveIndex(title,content,url);

    }

    /**
     * 获取url
     * @param file
     * @return
     */
    private String getUrl(File file) {
   
        StringBuilder stringBuilder = new StringBuilder();
        String str = file.getAbsolutePath().substring(PATH_ROOT.length()+1);
        for (int i = 0; i < str.length(); i++) {
   
            char ch = str.charAt(i);
            if (ch != '\\') {
   
                stringBuilder.append(ch);
            } else {
   
                stringBuilder.append('/');
            }
        }
        return JAVA_PATN+stringBuilder.toString();
    }

    /**
     * 获取正文,这个比较麻烦,我们需要去除标签,和<script></script>里面的内容
     * 这里我们需要使用正则表达式
     * @param file
     * @return
     */
    public String getContents(File file) {
   
        //获取到HTML里面的内容
        String content = getcontentHtml(file);
        //使用正则表达式,将<script></script>标签和里面的内容都替换掉
        //字符串中的replaceAll方法是支持正则表达式的
        content = content.replaceAll("<script.*?>(.*?)</script>"," ");
        //使用正则表达式,去除其他标签
        content = content.replaceAll("<.*?>"," ");
        //使用正则表达式,去除连续的空格
        content = content.replaceAll("\\s+"," ");
        return content ;
    }

    /**
     * 获取到HTML文件的内容&#x
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/862343?site
推荐阅读
相关标签
  

闽ICP备14008679号