Lucene创建索引(一)

这是工程目录

package index;

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

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.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.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.junit.Test;

public class testIndexOperation {
	@Test
	public void testCreateIndex() throws Exception{
		List list =  new ArrayList();
		//读取需要创建索引的文件
		File dir =  new File("C:\\Users\\sam\\Desktop\\lucene");
		//利用io包对其进行遍历
		for(File file:dir.listFiles()){
			Document doc =  new Document();
			String fileName = file.getName();
			Long fileSize = FileUtils.sizeOf(file);
			String fileContext = FileUtils.readFileToString(file);
			TextField f1 =  new TextField("fileName", fileName, Store.YES);
			TextField f2 =  new TextField("fileContext", fileContext, Store.YES);
			LongField f3 =  new LongField("fileSize", fileSize, Store.YES);
			doc.add(f1);
			doc.add(f2);
			doc.add(f3);
			list.add(doc);
		}
		//以上对文件进行分析完毕,创建分词器
		Analyzer analyzer = new StandardAnalyzer();
		//设置索引输出目录
		Directory directory =  FSDirectory.open(new File("C:\\Users\\sam\\Desktop\\dir"));
		//设置索引输出配置
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
		//创建索引输出器
		IndexWriter indexWriter = new IndexWriter(directory, config);
		//遍历document
		for(Document doc:list){
			indexWriter.addDocument(doc);
		}
		//提交
		indexWriter.commit();
		//关闭资源
		indexWriter.close();
	}
}
 


这是等待分析的文本


这是得到的索引文件。

索引文件可以利用lukeall打开进行查阅

以下是使用Lucene创建索引个完整代码示例。该示例展示了如何配置`IndexWriter`以及如何向索引中添加文档。 ### 使用Lucene创建索引的代码示例 ```java import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; 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 java.io.File; import java.nio.file.Paths; public class LuceneIndexExample { public static void main(String[] args) throws Exception { // 定义存储索引的位置 Directory directory = FSDirectory.open(Paths.get("indexDir")); // 创建StandardAnalyzer实例用于分词 StandardAnalyzer analyzer = new StandardAnalyzer(); // 配置IndexWriter IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter indexWriter = new IndexWriter(directory, config); // 创建Document对象 Document doc = new Document(); // 添加字段到Document中 String id = "1"; String content = "This is the content of a sample document."; doc.add(new StringField("id", id, Field.Store.YES)); doc.add(new TextField("content", content, Field.Store.NO)); // 将Document写入索引 indexWriter.addDocument(doc); // 提交更改并关闭IndexWriter indexWriter.commit(); indexWriter.close(); System.out.println("索引创建完成!"); } } ``` 这段代码实现了以下几个功能: - 设置索引存储位置,这里选择了文件系统中的指定路径[^2]。 - 初始化了个`StandardAnalyzer`来解析文本内容[^3]。 - 配置了`IndexWriter`及其对应的分析器[^5]。 - 构建了个包含两个字段(`id` 和 `content`)的`Document`对象,并将其添加至索引中[^4]。 #### 关键点说明 - `StringField` 是种不可分割的字段型,适合用来保存唯标识符等不需要进步处理的内容。 - `TextField` 则适用于需要被全文检索的文本数据,在加入索引前会被分析器分解成多个词条[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值