lucene简单的入门程序

本文深入解析了使用Lucene进行索引创建的过程,包括指定存储位置、创建分词器、配置IndexWriter及写入文档等关键步骤,并详细介绍了各对象的作用与用法。
public class Lucene {
	//创建索引程序
	@Test
	public void createIndex() throws IOException{
		//指定存储索引库位置
		String path = "F:\\indexs";
		//和索引库进行关联
		FSDirectory d = FSDirectory.open(new File(path));
		//创建分词器
		Analyzer analyzer = new StandardAnalyzer();
		//indexWriterConfig是对IndexWriter进行一些配置,lucene版本,最大缓存文档数
		IndexWriterConfig conf = new   IndexWriterConfig(Version.LUCENE_4_10_3,analyzer);
		//使用indexWriter进行写入索引
		IndexWriter indexWriter = new  IndexWriter(d, conf);
		//创建document,IndexWriter把文档对象写入到索引库
		Document doc = new Document();
		//模拟数据
		//StringField:这个域不进行分词,Store.YES:表示存储document。
		//Store.NO:表示不存储。
		doc.add(new StringField("id", "1", Store.YES));
		doc.add(new TextField("title", "Lucene基础教程", Store.YES));
		doc.add(new TextField("content","Lucene是一套用于开发搜索引擎的一套javaAPI!", Store.YES));
		//写入索引
		indexWriter.addDocument(doc);
		//提交
		indexWriter.commit();
		indexWriter.close();
	}
}

主要对象有Directory指定索引库目录位置, 参数为路径;

  Analyzer分词器(IKAnalyzer), 创建一个分词器;

IndexWriterConfig是对IndexWriter进行配置,并传入分词器

创建一个IndexWriter,写入索引的对象,参数为索引库关联对象, 配置文件 ;
创建Document对象加入域

写入索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值