码迷,mamicode.com
首页 > Web开发 > 详细

lucene 例子,lucene demo, lucene 教程

时间:2014-11-21 16:22:56      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:java   索引   搜索   lucene   

查了很多lucene资料,就纳闷为啥不能分享个简单的例子,我在这里自己写了一个

lucene实现其实很简单,先建立索引,在进行搜索,easy!

下载jar包,链接在这里:http://download.csdn.net/detail/dannor2010/8183641   项目中导入lib即可,不多说了。

前期:创建两个txt文件,
C:\\source
C:\\index
source 中建立txt文件,输入你想要测试搜索的String类型内容。

1、建立索引,代码如下

package com.ch.lucene;


import java.io.File;
import java.io.FileReader;
import java.util.Date;


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.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


import com.ch.util.FileUtil;


public class TextFileIndexer {


	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception {
		File fileDir = new File("C:\\source");
		File fileIndex = new File("C:\\index");


		Directory dir = FSDirectory.open(fileIndex);


		Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_45);


		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45,
				luceneAnalyzer);


		iwc.setOpenMode(OpenMode.CREATE);


		IndexWriter iw = new IndexWriter(dir, iwc);


		File[] textFiles = fileDir.listFiles();
		long startTime = new Date().getTime();


		// 增加document到索引去
		for (File file : textFiles) {
			if (file.isFile() && file.getName().endsWith(".txt")) {
				System.out
						.println("文件:" + file.getCanonicalPath() + "正在被索引操作。");
				String temp = FileUtil.readTxtFile(file.getPath());
				System.out.println(temp);
				Document document = new Document();
				@SuppressWarnings("unused")
				Field filepath = new Field("path", file.getPath(),
						Field.Store.YES, Field.Index.NO);
				Field FieldBody = new Field("body", temp, Field.Store.YES,
						Field.Index.ANALYZED,
						Field.TermVector.WITH_POSITIONS_OFFSETS);
				document.add(filepath);
				document.add(FieldBody);
				iw.addDocument(document);
			}
		}
		iw.close();


		// 测试一下索引的时间
		long endTime = new Date().getTime();
		System.out.println("这花费了" + (endTime - startTime) + " 毫秒来把文档增加到索引里面去"
				+ fileDir.getPath());
	}
}


2、进行搜索,代码如下

package com.ch.lucene;


import java.io.File;
import java.io.IOException;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


public class Query {
	public static void main(String[] args) throws IOException, ParseException {
		String index = "C:\\index";
		@SuppressWarnings("deprecation")
		IndexReader reader = IndexReader
				.open(FSDirectory.open(new File(index)));
		IndexSearcher searcher = new IndexSearcher(reader);
		ScoreDoc[] hits = null;
		String queryString = "关键词"; // 搜索的关键词
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
		QueryParser qp = new QueryParser(Version.LUCENE_45, "body", analyzer);
		org.apache.lucene.search.Query query = qp.parse(queryString);


		if (searcher != null) {
			TopDocs results = searcher.search(query, 10);
			hits = results.scoreDocs;
			if (hits.length > 0) {
				System.out.println("找到:" + hits.length);
			}
		}


	}


}





lucene 例子,lucene demo, lucene 教程

标签:java   索引   搜索   lucene   

原文地址:http://blog.csdn.net/dannor2010/article/details/41348507

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!