4. Lucene入门

Apache Lucene是一个用Java写的高性能、可伸缩的全文检索引擎工具包,它可以方便的嵌入到各种应用中实现针对应用的全文索引/检索功能。Lucene的目标是为各种中小型应用程序加入全文检索功能。
Lucene的核心作者:Doug Cutting是一位资深全文索引/检索专家。
版本发布情况:2000年3月,最初版发布,2001年9月,加入apache;2004年7月,发布1.4正式版;2009年11月,发布2.9.1(jdk1.4)及3.0(jdk1.5)版本;2015年3月,发布4.10.4。2016年2月,发布5.5.0。

导包
在这里插入图片描述
1、 把文本内容转换为Document对象
文本是作为Document对象的一个字段而存在
2、准备IndexWriter(索引写入器)
3 、通过IndexWriter,把Document添加到缓冲区并提交
addDocument
commit
close
//创建索引的数据 现在写死,以后根据实际应用场景
String doc1 = “hello world”;
String doc2 = “hello java world”;
String doc3 = “hello lucene world”;
private String path =“F:/eclipse/workspace/lucene/index/
hello”;
@Test
public void testCreate() {
try {
//2、准备IndexWriter(索引写入器)
//索引库的位置 FS fileSystem
Directory d = FSDirectory.open(Paths.get(path ));
//分词器
Analyzer analyzer = new StandardAnalyzer();
//索引写入器的配置对象
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(d, conf);
System.out.println(indexWriter);

		//1、 把文本内容转换为Document对象
		//把文本转换为document对象
		Document document1 = new Document();
		//标题字段
		document1.add(new TextField("title", "doc1", Store.YES));
		document1.add(new TextField("content", doc1, Store.YES));
		//添加document到缓冲区
		indexWriter.addDocument(document1);
		Document document2 = new Document();
		//标题字段
		document2.add(new TextField("title", "doc2", Store.YES));
		document2.add(new TextField("content", doc2, Store.YES));
		//添加document到缓冲区
		indexWriter.addDocument(document2);
		Document document3 = new Document();
		//标题字段
		document3.add(new TextField("title", "doc3", Store.YES));
		document3.add(new TextField("content", doc3, Store.YES));
		
		//3 、通过IndexWriter,把Document添加到缓冲区并提交
		//添加document到缓冲区
		indexWriter.addDocument(document3);
		indexWriter.commit();
		indexWriter.close();
		
	} catch (Exception e) {
		e.printStackTrace();
	}

}

   // OpenMode=create 每次都会重置索引库然后重新添加索引文档
   // 后者覆盖前者(默认是不覆盖累加模式)
	conf.setOpenMode(OpenMode.CREATE);

图形界面客户端使用
4.2.2. 搜索索引
1 封装查询提交为查询对象
2 准备IndexSearcher
3 使用IndexSearcher传入查询对象做查询-----查询出来只是文档编号DocID
4 通过IndexSearcher传入DocID获取文档
5 把文档转换为前台需要的对象 Docment----> Article

@Test
	public void testSearch() {
		String keyWord = "lucene";
		try {
			// * 1 封装查询提交为查询对象
		    //通过查询解析器解析一个字符串为查询对象
			String f = "content"; //查询的默认字段名,
			Analyzer a = new StandardAnalyzer();//查询关键字要分词,所有需要分词器
			QueryParser parser = new QueryParser(f, a);
			Query query = parser.parse("content:"+keyWord);
			// * 2 准备IndexSearcher
			Directory d = FSDirectory.open(Paths.get(path ));
			IndexReader r = DirectoryReader.open(d);
			IndexSearcher searcher = new IndexSearcher(r);
			// * 3 使用IndexSearcher传入查询对象做查询-----查询出来只是文档编号DocID
			TopDocs topDocs = searcher.search(query, 1000);//查询ton条记录 前多少条记录
			System.out.println("总命中数:"+topDocs.totalHits);
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;//命中的所有的文档的封装(docId)
			// * 4 通过IndexSearcher传入DocID获取文档
			for (ScoreDoc scoreDoc : scoreDocs) {
				int docId = scoreDoc.doc;
				Document document = searcher.doc(docId);
				// * 5 把文档转换为前台需要的对象 Docment----> Article
				System.out.println("=======================================");
				System.out.println("title:"+document.get("title")
								+",content:"+document.get("content"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值