Lunece4.2例子

package com.test.luncene;

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

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.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.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.util.Version;

/**
 * @author linchers
 *
 */
public class HelloLuncene {

	/**
	 *  建立索引
	 */
	public void createIndex(){
		
		//创建IndexWriter
		IndexWriter iw = null;
		try {
			//创建Directory
			//Directory dir = new RAMDirectory();  //放到内存中
			Directory dir = FSDirectory.open(new File("D://luncene/index01")); //放到硬盘中
			//创建IndexWriter
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);
			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_42,analyzer);

			iw = new IndexWriter(dir,iwc);
			//创建Document对象
			Document doc = null;
			//为Document对象添加Field
			File files = new File("D://luncene/simple");
			FieldType ft = new FieldType();
			//ft.setStored(true);
			ft.setIndexed(true);
			for(File file :files.listFiles())
			{
				doc = new Document();
				doc.add(new Field("content",new FileReader(file),ft));
				doc.add(new StringField("filename",file.getName(),Store.YES));
				doc.add(new StringField("path",file.getAbsolutePath(),Store.YES));
				System.out.println("filename = "+file.getName() + "  |  path =" + file.getAbsolutePath());
				iw.addDocument(doc);
			}
			//通过IndexWriter添加文档到索引中
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				iw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 执行搜索
	 */
	public void searcher(){
		
		try {
			//创建Directory
			Directory dir = FSDirectory.open(new File("D://luncene/index01"));

			//创建IndexReader
			IndexReader ir = DirectoryReader.open(dir);
			
			//根据IndexReader 创建 IndexSearcher
			IndexSearcher is = new IndexSearcher(ir);
			
			//创建搜索的Query
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);

			QueryParser qp = new QueryParser(Version.LUCENE_42,"content",analyzer);
			
			Query query = qp.parse("span");
			
			//根据searcher搜索 返回TopDocs
			TopDocs topDocs = is.search(query, 10);
			
			//根据TopDoc 获取 ScoreDoc
			ScoreDoc[] docs = topDocs.scoreDocs;
			Document document = null;
			//根据searcher 和 ScoreDoc 获取具体的Document 对象
			for(ScoreDoc doc :docs){
				//根据document对象获取需要的值
				document = is.doc(doc.doc);
				System.out.println(document.get("filename")+"  ######  "+document.get("path"));
			}
		
			//关闭reader
			ir.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
	
}

 测试类

import junit.framework.TestCase;

/**
 * @author linchers
 *
 */
public class HelloLunceneTest extends TestCase {


	@Test
	public void testCreateIdex() {
		HelloLuncene hl = new HelloLuncene();
		hl.createIndex();
	}
	
	@Test
	public void testSearcher(){
		HelloLuncene hl = new HelloLuncene();
		hl.searcher();
		
	}

}

 

你可能感兴趣的:(lucene 4.2)