lucene创建索引库

使用lucene创建索引库需要的jar包:

maven形式:

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>8.0.0</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>8.0.0</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>8.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>

并导入相应的IK中文分词器的jar包

创建索引库:

首先获取原始文件:

创建Directory目录对象,用来指向索引库存放路径

先创建IndexWriterConfig对象用来指定分词器,然后创建用来写入索引的IndexWriter对象,然后获取原始文件,并创建文档对象,把原始文件的信息存储到对应的域中,把这些域放入文档中,并使用IndexWriter对象把该文档对象写入到索引库中,并关流;代码实现如下:

        //创建一个indexwriter对象
        Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
        Analyzer analyzer = new IKAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        //创建field域
        File file = new File("D:\\Java\\lucene-test");
        File[] files = file.listFiles();
        for (File file2 : files) {
            //创建document对象
            Document document = new Document();
            //文件名域
            String fileName = file2.getName();
            //分析 Y 添加索引 Y 是否保存 Y/N
            Field fileNameField = new TextField("fileName", fileName, Store.YES);
            document.add(fileNameField);
            //文件路径
            String filePath = file2.getPath();
            // 分析N 添加索引Y 是否保存 Y/N
            Field filePathField = new StringField("filePath", filePath, Store.YES);
            document.add(filePathField);
            //文件大小
            long fileSize = FileUtils.sizeOf(file2);
            // 分析N 添加索引N 是否保存N
            Field fileSizeField = new StoredField("fileSize", fileSize);
            document.add(fileSizeField);
            //文件内容
            String fileContext = FileUtils.readFileToString(file2);
            // 分析Y 添加索引 Y 是否保存 Y/N
            Field fileContextField = new TextField("fileContext", fileContext, Store.NO);
            document.add(fileContextField);
            indexWriter.addDocument(document);
        }
        //关闭indexWriter
        indexWriter.close();

 

代码实现图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值