lucene3.0.0 入门实例 创建索引 查询.

本文介绍如何使用 Lucene 3.0.0 创建索引及搜索文档,通过具体示例展示了创建索引的过程及如何进行简单的查询操作。

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

[align=center][size=x-large][color=red]lucene3.0.0 入门实例 创建索引 查询.[/color][/size][/align]package com.txt.test2;

 

import java.io.File;

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.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class LuceneTest {
	String txt1 = "holle world chengdu";
	String txt2 = "holle world sichuang chengdu";
	String txt3 = "holle world sichuang chengdu chengdu";
	String txt4 = "holle world sichuang chengdu chengdu chengdu";

	private File file = new File("f:" + File.separator + "indexDir6");

	@Test
	public void create() throws Exception {
		// 索引库存储目录
		Directory directory = new SimpleFSDirectory(file);
		// 创建词元分析器
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
		// 创建索引器
		IndexWriter writer = new IndexWriter(directory, analyzer, true,
				MaxFieldLength.LIMITED);

		// 创建文档
		/**
		 * 参数1 : 字段名称 相当于数据库的字段名称 参数2 :数据源名称 参数3 : 是否要把数据源名称存储到索引库中Store.YES 参数4
		 * : 是否要使用分词器作分析Index.ANALYZED
		 */
		Document document = new Document();
		document.add(new Field("content", txt1, Store.YES, Index.ANALYZED));
		Document document2 = new Document();
		document2.add(new Field("content", txt2, Store.YES, Index.ANALYZED));
		Document document3 = new Document();
		document3.add(new Field("content", txt3, Store.YES, Index.ANALYZED));
		Document document4 = new Document();
		document4.add(new Field("content", txt4, Store.YES, Index.ANALYZED));

		writer.addDocument(document);
		writer.addDocument(document2);
		writer.addDocument(document3);
		writer.addDocument(document4);

		writer.close();
	}

	@Test
	public void search() throws Exception {
		// 索引存储目录
		Directory directory = new SimpleFSDirectory(file);
		// 创建查询器
		IndexSearcher searcher = new IndexSearcher(directory, true);
		// 创建查询解析器 对文档document的content字段进行查询
		QueryParser parser = new QueryParser(Version.LUCENE_30, "content",
				new StandardAnalyzer(Version.LUCENE_30));
		//创建查询类
		String key = "chengdu";
		Query query = parser.parse(key);
		//获取钱n位索引
		TopDocs tDocs = searcher.search(query, 100);
		System.out.println("关键字"+key+"一共有"+tDocs.totalHits+"条记录索引.");
		for(int i = 0; i<tDocs.scoreDocs.length; i++){
			ScoreDoc sDoc = tDocs.scoreDocs[i];
			System.out.println("文档编号的索引:"+sDoc.doc);
			System.out.println("得分:"+sDoc.score);
			
			Document document = searcher.doc(sDoc.doc);
			System.out.println("内容是:"+document.get("content"));
		}
		searcher.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值