lucene创建索引

实现步骤

第一步:创建一个indexwriter对象。

1)指定索引库的存放位置Directory对象

2)指定一个分析器,对文档内容进行分析。

第二步:创建document对象。

第三步:创建field对象,将field添加到document对象中。

第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。

第五步:关闭IndexWriter对象。

Field域属性

代码实现

package com.test.lucene.helloworld;

import java.io.File;

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

public class LuceneTestCreateIndex {

    // 创建索引
    @Test
    public void createIndex() throws Exception {

        // 指定索引库存放的路径
        // E:\\study\\test\\index FSD(File System Directory)
        // 指定路径如果不存在会自动创建open(new File("E:\\study\\test\\index")
        Directory directory = FSDirectory.open(new File("E:\\study\\test\\index"));
        // 索引库还可以存放到内存中
        // Directory directory = new RAMDirectory();
        // 创建一个标准分析器
        Analyzer analyzer = new StandardAnalyzer();
        // 创建indexwriterCofig对象
        // 第一个参数: Lucene的版本信息,可以选择对应的lucene版本也可以使用LATEST
        // 第二根参数:分析器对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
        // 创建indexwriter对象
        IndexWriter indexWriter = new IndexWriter(directory, config);
        // 原始文档的路径E:\study\test\searchsource
        File dir = new File("E:\\study\\test\\searchsource");
        File[] listFiles = dir.listFiles();
        for (File f : listFiles) {
            // 文件名
            String fileName = f.getName();
            // 文件内容FileUtils文件的工具类,来自org.apache.commons.io包
            String fileContent = FileUtils.readFileToString(f);
            // 文件路径
            String filePath = f.getPath();
            // 文件的大小
            long fileSize = FileUtils.sizeOf(f);
            // 创建文件名域
            // 第一个参数:域的名称
            // 第二个参数:域的内容
            // 第三个参数:是否存储
            Field fileNameField = new TextField("filename", fileName, Store.YES);
            // 文件内容域(分析、索引、存储)
            Field fileContentField = new TextField("content", fileContent, Store.YES);
            // 文件路径域(不分析、不索引、只存储)
            Field filePathField = new StoredField("path", filePath);
            // 文件大小域
            Field fileSizeField = new LongField("size", fileSize, Store.YES);

            // 创建document对象
            Document document = new Document();
            document.add(fileNameField);
            document.add(fileContentField);
            document.add(filePathField);
            document.add(fileSizeField);
            // 创建索引,并写入索引库.干了两件事情(存储索引和文档对象)
            indexWriter.addDocument(document);
        }
        // 关闭indexwriter
        indexWriter.close();
    }
}

 

转载于:https://www.cnblogs.com/jepson6669/p/9051643.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lucene是一个开源的全文搜索引擎库,它提供了丰富的API和功能来创建、更新和搜索索引。要更新Lucene索引,首先需要获取一个IndexWriter实例,然后通过该实例来执行索引更新操作。 更新索引的过程通常包括以下几个步骤: 1. 创建或获取一个IndexWriter实例:IndexWriter负责索引的写入和更新操作,需要确保只有一个线程能够同时操作一个IndexWriter实例。 2. 创建或获取文档对象:更新索引需要构建文档对象,可以通过Document类来创建一个文档对象,并向其添加字段。 3. 执行更新操作:可以通过IndexWriter的addDocument、updateDocument或deleteDocuments方法来执行索引的添加、更新或删除操作。 4. 提交更新:在更新索引操作完成后,需要调用IndexWriter的commit或flush方法来提交更新,使更新操作生效。 更新索引的频率取决于应用的需求,可以是实时更新,也可以是批量更新。总之,更新索引是一个重要的操作,在使用Lucene构建搜索引擎或全文检索功能时,需要根据具体的业务需求来合理地更新索引。同时,为了保证索引更新的性能和可靠性,需要注意合理地管理IndexWriter实例,确保其在适当的时候被关闭或重新打开。 总之,通过Lucene的API和功能,可以方便地进行索引的更新操作,从而保证应用的搜索功能能够及时、准确地返回符合用户需求的搜索结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值