第一个Lucene实例

运行lucene所需要的JAR包
lucene-core-3.6.0.jar(核心包)
lucene-analyzers-3.6.0.jar(分词器)
lucene-highlighter-3.6.0.jar(高亮)
lucene-memory-3.6.0.jar(高亮)

public class HelloWord {
	public static void createIndexFile() {
		IndexWriter indexWriter=null;
		try {
			// 需要的分词器
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
			// 创建的是哪个版本的IndexWriterConfig
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
					Version.LUCENE_36, analyzer);
			// 创建系统文件----- ./ 当前路径下的
			Directory directory = new SimpleFSDirectory(new File("./indexDir/"));
			indexWriter = new IndexWriter(directory,indexWriterConfig);	
			//获取实体对象
			Article article=new Article(11,"最XX的城市","XX");	
			//indexWriter添加索引
			Document doc=new Document();
			//文本中添加内容            标题      内容
			/*doc.add(new Field("title","中国的首都在哪里",Store.YES,Index.ANALYZED));
			doc.add(new Field("content","中国的首都在北京",Store.YES,Index.ANALYZED));*/	
			doc.add(new Field("id",article.getId().toString(),Store.YES,Index.ANALYZED));
			doc.add(new Field("title",article.getTitle().toString(),Store.YES,Index.ANALYZED));
			doc.add(new Field("content",article.getContent().toString(),Store.YES,Index.ANALYZED));	
			//添加到索引中去
			indexWriter.addDocument(doc);	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(indexWriter!=null){
				try {
					indexWriter.close();
				}  catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	//如果查询是需要用到解析器,那解析器必须和创建时的解析器相同
	public static void searchIndexFileResult() throws IOException {	
		List<Article> articles=new ArrayList<Article>();	
		//得到索引的目录
		Directory directory = new SimpleFSDirectory(new File("./indexDir/"));
		//根据目录打开一个indexReader
		IndexReader indexReader=IndexReader.open(directory);
		//System.out.println(indexReader.maxDoc());	
		//获取最小值的document对象
		//Document doc=indexReader.document(0);
		//获取最大值的document对象
		//Document doc=indexReader.document(indexReader.maxDoc()-1);
		//document对象的get(字段名称)方法获取字段的值
		/*System.out.println(doc.get("id"));
		System.out.println(doc.get("title"));
		System.out.println(doc.get("content"));*/	
		int n=indexReader.maxDoc();
		for(int i=0;i<n;i++){
			Document doc=indexReader.document(i);
			Article article=new Article();
			if(doc.get("id")==null){
				System.out.println("id为空");
			}else{
				article.setId(Integer.parseInt(doc.get("id")));
				article.setTitle(doc.get("title"));
				article.setContent(doc.get("content"));
				articles.add(article);
			}
		}
		for(Article article:articles){
			System.out.println(article.toString());
		}	
	}
	public static void main(String[] args) throws IOException {
		// 建立要索引的文件
	//	createIndexFile();
		// 从索引文件中查询数据
		searchIndexFileResult();
		// 获得结果,然后交由相关应用程序处理
	}
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值