02_Lucene创建索引

案例演示

  • 新建java项目,命名lucene

  • 新建lib目录,导入
    commons-io-2.4.jar 文件IO的jar包
    junit-4.9.jar junit测试
    lucene-analyzers-common-4.10.3.jar 分词的jar包
    lucene-core-4.10.3.jar 核心的jar包
    lucene-queryparser-4.10.3.jar 查询的jar包

  • 创建索引

package cn.huahcao.lucene;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class IndexManagerTest {
    @Test
    public void testCreateIndex() throws Exception{
        //采集文件系统中的文档数据,放入lucene中
        //文档列表,保存Document
        List<Document> docList = new ArrayList<Document>();

        //指定文件所在的目录
        File dir = new File("G:\\Java\\JavaEE\\09_SSM\\lucene_day01\\参考资料\\searchsource");

        //循环取出文件
        for (File file:dir.listFiles()){
            //文件名称
            String fileName = file.getName();
            //文件内容
            String fileContent = FileUtils.readFileToString(file);
            //文件大小
            Long fileSize = FileUtils.sizeOf(file);
            //文档对象。文件系统中的一个文件就是一个Document对象
            Document doc = new Document();
            /**
             * 第一个参数:域名
             * 第二个参数:域值
             * 第三个参数:是否存储,是为Yes,不存储为No
             */
            TextField nameField = new TextField("fileName",fileName, Field.Store.YES);
            TextField contentField = new TextField("fileContent",fileContent, Field.Store.YES);
            TextField sizeField = new TextField("fileSize",fileSize.toString(), Field.Store.YES);

            //将所有的域存入文档中
            doc.add(nameField);
            doc.add(contentField);
            doc.add(sizeField);

            //将文档存入文档集合中
            docList.add(doc);
        }

        //创建分词器,StandardAnalyzer标准分词器,标准分词器对英文分词效果很好,对中文是单字分词
        StandardAnalyzer analyzer = new StandardAnalyzer();
        //指定索引和文档存储的目录
        FSDirectory directory = FSDirectory.open(new File("G:\\Java\\JavaEE\\09_SSM\\lucene_day01\\tmp"));
        //创建写对象的初始化对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3,analyzer);
        //创建索引和文档写对象
        IndexWriter indexWriter = new IndexWriter(directory , config);

        //将文档加入到索引和文档的写对象中
        for (Document doc:docList){
            indexWriter.addDocument(doc);
        }
        //提交
        indexWriter.commit();
        //关闭流
        indexWriter.close();
    }
}

运行上面的testCreateIndex(),生成索引


使用工具luke查看

  • 打开luke所在目录

    luke所在的目录不能保护中午和空格

    luke所在的目录不能保护中午和空格

  • 运行工具

    java -jar lukeall-4.10.3.jar

    java -jar lukeall-4.10.3.jar运行

  • 打开的界面如下


  • 设置为索引所在的目录,然后点击OK


注意选择FSDirectory,因为我们代码中使用的是FSDirectory。

  • 打开了索引查看的界面


  • 显示某个Field的分词情况


  • 查看Document


  • 搜索


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容