lucene3.6.0——入门示例

lucene3.6.0入门示例,项目所需的jar包如下:

    1.lucene-core-3.6.0.jar(Lucene核心包)

    2.lucene-analyzers-3.6.0.jar(Lucene分词器)

    3.IKAnalyzer3.2.8.jar(中文分词器)

    4.Junit4(单元测试)

下面直接上源码:

    1.MyLucene.java

package test;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;

public class MyLucene {

	private static final File INDEX_PATH = new File(".\\index");		// 索引文件位置, 当前路径下的index文件
	private static final  String filePath = ".\\luceneDataSource\\test.txt";// 索引数据源文件位置,当前路径下的luceneDataSource\test.txt文件
	private static final Analyzer ANALYZER = new IKAnalyzer(); 		// 中文分词器

	
	public MyLucene(){
	}
	
	/**
	 * 创建索引
	 */
	public void CreateIndex() {
		File readFile = new File(filePath);									// 获取数据源文件
		HashMap<String, String> words = readFile(readFile);

		Document doc = null;
		if (words != null) {
				try {
						IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, ANALYZER);
						IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_PATH), writerConfig);
		
						Set<String> keys = words.keySet();
		
						for (Iterator<String> it = keys.iterator(); it.hasNext();) {
							String key = it.next();
							doc = new Document();
							Field index = new Field("index", key, Field.Store.YES,Field.Index.ANALYZED);
							Field contents = new Field("contents", words.get(key),Field.Store.YES, Field.Index.NO);
							doc.add(index);
							doc.add(contents);
							writer.addDocument(doc);
						}
						writer.close();	// 这里不关闭建立索引会失败
				} catch (Exception e) {
						e.printStackTrace();
				}
		} 
		else 
				System.out.println("文件读取错误");

	}
	
	/**
	 * 判断索引库是已否创建
	 */
	public boolean noIndex() {
			File[] indexs = INDEX_PATH.listFiles();
			if (indexs.length == 0) {
				return true;
			} else {
				return false;
			}
	}
	
	/**
	 * 读取文件
	 * @param file
	 */
	public HashMap<String, String> readFile(File file) {
		InputStream in = null;
		InputStreamReader inR = null;
		BufferedReader br = null;
		HashMap<String, String> wordsMap = new HashMap<String, String>();
		try {
				in = new FileInputStream(file);
				inR = new InputStreamReader(in, "utf-8");
				br = new BufferedReader(inR);
				String line;
				while ((line = br.readLine()) != null) {
					wordsMap.put(line.trim(), line.trim());
				}
				return wordsMap;

		} catch (Exception e) {
				e.printStackTrace();
				return null;
		} finally {
			try {
				if (in != null)
					in.close();
				if (inR != null)
					inR.close();
				if (br != null)
					br.close();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	}
	
	/**
	 * 检索
	 * @param queryStr
	 * @param hitsPerPage
	 */
	public void search(String queryStr) {
		
		try {
			IndexReader reader = IndexReader.open(FSDirectory.open(INDEX_PATH));// 得到索引的目录
			IndexSearcher searcher = new IndexSearcher(reader);

			Query query = IKQueryParser.parse("index", queryStr);
			TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);
			searcher.search(query, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;

			if(hits.length > 0){
				System.out.println("检索词:"+queryStr+"\t共找到 "+hits.length+"条记录");
				for (int i = 0; i < hits.length; i++) {
					Document result = searcher.doc(hits[i].doc);
					System.out.println((i+1) +")" + "\n  index:" + result.get("index") + "\n  contents:" + result.get("contents"));
				}
			}else{
				System.out.println("未找到结果");
			}
		} catch (Exception e) {
			System.out.println("Exception");
		}
	}
}

2.TestMyLucene.java

package test;

import org.junit.Test;

public class TestMyLucene {
	
	@Test
	public  void test() {
		MyLucene myLucene = new MyLucene();
		// 索引库是已否创建,如果没有则创建
		if(myLucene.noIndex()){ 		
			myLucene.CreateIndex();
		}
		myLucene.search("Lucene");
		
	}
}

 

测试结果:

lucene3.6.0——入门示例_第1张图片

 

 

你可能感兴趣的:(Lucene,全文检索,入门实例,Lucene3.6.0)