lucene 创建索引和搜索

1.lucene创建索引和搜索,主要用到一下几个类,IndexWriter,Document,Analyzer;IndexSearcher,QueryParser,Query,TopDocs,

2.通过FSDirectory和RAMDirectory的并用,可以提高速度。先把磁盘上的索引文件载入内存,然后在内存操作,免去了IO操作,可以提高效率,最后退出时,要把内存操作的结果保存在磁盘上。

3.fsIndexWriter.optimize();优化索引文件,把多个cfs文件合并成一个

4.建立索引和进行搜索时应该使用同一个分词器。


ps:用到的jar包:/LuceneDemo/lib/je-analysis-1.5.3.jar(中文分词器)
/LuceneDemo/lib/lucene-analyzers-2.4.0.jar(lucenne自带的)
/LuceneDemo/lib/lucene-core-2.4.0.jar
/LuceneDemo/lib/lucene-highlighter-2.4.0.jar

package com.bjsxt.helloworld;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
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.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import com.bjsxt.utils.File2DocumentUtil;

public class HelloWorld {

	/**
	 * @param args
	 */
	String filePath = "D:\\flexWorkespace\\LuceneDemo\\luceneDataSource\\IndexWriter addDocument's a javadoc .txt";
	String indexPath = "D:\\flexWorkespace\\LuceneDemo\\indexPath";

	Analyzer analyzer = new StandardAnalyzer();

	public static void main(String[] args) throws Exception {
		new HelloWorld().createIndexByDir();
		new HelloWorld().searchByDir("document");
	}

	public void createIndex() throws Exception {
		// File file = new File(filePath);
		Document doc = File2DocumentUtil.file2Document(filePath);

		// IndexWriter 是用来操作(增删改)索引库的
		IndexWriter iw = new IndexWriter(indexPath, analyzer, true,
				MaxFieldLength.LIMITED);
		iw.addDocument(doc);
		iw.close();
	}
	
	public void createIndexByDir()throws Exception {
		
		//1.创建时候载入文件系统里的索引
		Directory fsDir = FSDirectory.getDirectory(indexPath);
		Directory ramDir = new RAMDirectory(fsDir);
		//new ramIndexWriter时不需要重新创建
		IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
		//添加document
		Document doc = File2DocumentUtil.file2Document(filePath);
		ramIndexWriter.addDocument(doc);
		ramIndexWriter.close();
		
		//2.退出时保存内存里的索引
		//new fsIndexWriter时需要重新创建,即删除原来的索引文件
		IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, true, MaxFieldLength.LIMITED);
//		Directory[] dir = {ramDir};                       
		fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
                  //把内存里的东西提交后才优化
		fsIndexWriter.commit();
                  // 优化索引文件,把多个cfs文件合并成一个 
                  fsIndexWriter.optimize();      
		fsIndexWriter.close();
	}
	
	public void search(String queryStr) throws Exception {
		String[] fields = { "name", "content" };
		QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
		Query query = null;
		Filter filter = null;
		query = queryParser.parse(queryStr);
		IndexSearcher indexSearcher = null;
		indexSearcher = new IndexSearcher(indexPath);
		TopDocs topDocs = indexSearcher.search(query, filter, 1000);
		System.out.println("总共有" + topDocs.totalHits + "条记录:");
		for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
			int docNum = scoreDoc.doc;
			Document doc = indexSearcher.doc(docNum);
			File2DocumentUtil.printDocumentInfo(doc);
		}
	}
	
}


工具类:
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

public class File2DocumentUtil {
	public static Document file2Document(String filePath) throws Exception {
		File file = new File(filePath);

		Document doc = new Document();
		doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
		doc.add(new Field("content", readFileContent(file), Store.YES,
				Index.ANALYZED));
		doc.add(new Field("size", String.valueOf(file.length()), Store.YES,
				Index.NOT_ANALYZED));
		doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NO));

		return doc;
	}

	private static String readFileContent(File file) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer content = new StringBuffer();
		BufferedReader br = new BufferedReader(new FileReader(file));
		for (String line = null; (line = br.readLine()) != null;) {
			content.append(line).append("\n");
		}

		return content.toString();
	}

	public static void printDocumentInfo(Document doc) {
		System.out.println("------------------------------");
		System.out.println("name:" + doc.get("name"));
		System.out.println("content:" + doc.get("content"));
		System.out.println("path:" + doc.get("path"));
		System.out.println("size:" + doc.get("size"));
	}
}

你可能感兴趣的:(apache,Lucene)