Lucene学习笔记之创建Index

本文介绍如何使用Lucene 2.4.0版本创建和查询文本索引。通过示例代码展示了创建包含三份文档的索引,并对所有文档进行检索的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.management.Query;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocCollector;

/*
 * 学习笔记之创建Index
 * 
 * Lucene 版本 2.4.0
 */
public class LuceneJE1 {

	private String path = "./index";

	public void createIndex() throws Exception {
		// 相当于打开了一个数据库的连接准备写入数据
		// 注意:如果第三个参数为false并且path没有索引文件
		// 就会抛出一个IOException
		IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),
				true, MaxFieldLength.UNLIMITED);

		// 相当于数据库里行的概念,这里创建了3行数据
		Document doc1 = new Document();
		Document doc2 = new Document();
		Document doc3 = new Document();

		// Field相当于数据库中列的概念,这里给一行的数据赋值
		doc1.add(new Field("id", "1", Field.Store.YES, Field.Index.ANALYZED));
		doc1.add(new Field("name", "cobain", Field.Store.YES,
				Field.Index.ANALYZED));
		doc1.add(new Field("content", "hello", Field.Store.YES,
				Field.Index.ANALYZED));

		doc2.add(new Field("id", "2", Field.Store.YES, Field.Index.ANALYZED));
		doc2.add(new Field("name", "rose", Field.Store.YES,
				Field.Index.ANALYZED));
		doc2.add(new Field("content", "hi", Field.Store.YES,
				Field.Index.ANALYZED));

		doc3.add(new Field("id", "3", Field.Store.YES, Field.Index.ANALYZED));
		doc3.add(new Field("name", "mike", Field.Store.YES,
				Field.Index.ANALYZED));
		doc3.add(new Field("content", "how are you", Field.Store.YES,
				Field.Index.ANALYZED));

		// 相当于把数据写入数据库
		writer.addDocument(doc1);
		writer.addDocument(doc2);
		writer.addDocument(doc3);
		// 对写入的数据进行优化,比如压缩Document的索引编号
		// 不要在服务器繁忙时对海量数据进行优化
		// 最好在服务器空闲或负载小的时候进行
		writer.optimize();
		// 相当与关闭数据库连接
		writer.close();
	}

	/*
	 * 这里把刚才建立的数据都查出来
	 */
	public void search() throws Exception {
		Searcher searcher = new IndexSearcher(path);
		MatchAllDocsQuery query = new MatchAllDocsQuery();
		TopDocCollector results = new TopDocCollector(10);
		searcher.search(query, results);
		ScoreDoc[] docs = results.topDocs().scoreDocs;
		for (int i = 0; i < docs.length; i++) {
			System.out.println("Document id:" + docs[i].doc);
			System.out.println("Document score:" + docs[i].score);
			System.out.println("Document:" + searcher.doc(i));
		}

	}

	public static void main(String[] args) throws Exception {
		LuceneJE1 je1 = new LuceneJE1();
		je1.createIndex();
		je1.search();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值