Lucene的简单应用

照着baidu写着Lucene的例子,突然发现new PaodingAnalyzer()不是Lucene包下的类,一查才知道他是paoding解牛的一个用于Lucene的工具,提供中文支持的,需要加入jar包:paoding-analysis.jar。其中要把该jar包中的paoding-dic-home.properties文件中的paoding.dic.home写成paoding.dic.home=F:\\jar\\Paoding\\dic形式(别忘了把注解去了)。dic为解压的dic文件夹。

创建索引例子代码如下:

package lucene;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

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.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

public class IndexWriterTest {
	
	public static final String indexDir = "src//core//scratch//";
	public static final String dataDir = "src//lucene//index//";

	public static void main(String[] args) throws FileNotFoundException,IOException,CorruptIndexException,LockObtainFailedException {
		File indexFile = new File(indexDir);
		Analyzer luceneAnalyzer = new StandardAnalyzer();
		IndexWriter indexWriter = new IndexWriter(dataDir, luceneAnalyzer, true);
		for(File file : indexFile.listFiles()){
			Reader reader = new FileReader(file);
			Document document = new Document();
			document.add(new Field("path",file.getCanonicalPath(), Field.Store.YES,Field.Index.UN_TOKENIZED));
			document.add(new Field("contents", reader));
			indexWriter.addDocument(document);
		}
		indexWriter.optimize();
		indexWriter.close();
		System.out.println("complete...");
	}
}


查询代码如下:

package lucene;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;

public class IndexSearcherTest {
	
	public static final String queryString = new String("m");
	
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		try {
			FSDirectory directory = FSDirectory.getDirectory(IndexWriterTest.dataDir, false);
			IndexSearcher searcher = new IndexSearcher(directory);
			Term term = new Term("contents",queryString.toLowerCase());
			TermQuery luceneQuery = new TermQuery(term);
			Hits hits = searcher.search(luceneQuery);
			for(int i = 0; i < hits.length(); i++){
				Document document = hits.doc(i);
				System.out.println("File: " + document.get("path"));
			} 
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("complete...");
	}

}

你可能感兴趣的:(源码,索引,学习,Lucene,创建索引)