zoukankan      html  css  js  c++  java
  • Lucene索引的初步创建

      从百度上知道的,Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供。Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。在Java开发环境里Lucene是一个成熟的免费开源工具。就其本身而言,Lucene是当前以及最近几年最受欢迎的免费Java信息检索程序库。人们经常提到信息检索程序库,虽然与搜索引擎有关,但不应该将信息检索程序库与搜索引擎相混淆。

    用的是Lucene 5.5.0版本,下载地址http://archive.apache.org/dist/lucene/java/5.5.0/

    一,下载后,需要找到Lucene需要的核心jar包

    lucene-analyzers-common-5.5.0.jar
    
    lucene-core-5.5.0.jar
    
    lucene-queryparser-5.5.0.jar

    找到核心jar包后,导入到开发项目的lib下。

    二、建立索引的步骤

    1、创建Directory

    2、创建IndexWriter

    3、创建Document对象

    4、往Document添加Field

    5、通过IndexWriter添加文档到索引中

    三、实例开发

    其中:FSDirectory.open(Paths.get("D://lucene//index"));  是将索引建立在自己电脑上硬盘上。

            如果建立在内存中,就可以使用Directory directory = new RAMDirectory();//建立在内存中

       /**
         * 建立索引
         */
        public void createIndex(){
            //1,创建Directory
            Directory directory = null;
            //2,创建IndexWriter
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = null;
            try {
                directory = FSDirectory.open(Paths.get("D://lucene//index"));  
                indexWriter = new IndexWriter(directory, config);
                //3,创建Document对象
                Document doc = null;
                //4,位Document添加Field
                File f = new File("D://text");
                for(File file:f.listFiles()){
                    doc = new Document();
                    @SuppressWarnings("deprecation")
                    Field content = new Field("content", new FileReader(file));
                    @SuppressWarnings("deprecation")
                    Field filename = new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    @SuppressWarnings("deprecation")
                    Field pathFile = new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    doc.add(content);
                    doc.add(filename);
                    doc.add(pathFile);
                    //5,通过IndexWriter添加文档到索引中
                    indexWriter.addDocument(doc);
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(indexWriter != null)
                    try {
                        indexWriter.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
            
        }

    执行后,可以看到索引文件:

     OK,索引就建好了。

  • 相关阅读:
    SpringBoot接口文件findBy方法写错导致抛出IllegalArgumentException
    用tomcat启动spring-boot
    Idea使用tool window中的persistence功能一键生成数据库实体
    Idea 启动 tomcat 报错
    详细梳理ajax跨域4种解决方案
    他们权利意识强,但是也会为生活牺牲很多
    每月碎碎念 | 2019.09
    说好不哭 我不会走
    css实现内容不相同的左右两个div等高
    简单了解css3轮廓outline
  • 原文地址:https://www.cnblogs.com/invban/p/6145685.html
Copyright © 2011-2022 走看看