`
smallearth
  • 浏览: 34564 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Lucene小练五(复习index)

阅读更多
package org.se.lucene;
//主类
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


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

public class hellowLucene {

	public void index()
	{
		IndexWriter writer=null;
		try {
			//1.创建directory
			Directory directory=FSDirectory.open(new File("F/lucene/Index03"));
			//2.创建IndexWriter
			 writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_36,
					new StandardAnalyzer(Version.LUCENE_36)));
			//3.创建Document对象
			Document document=null;
			//4.为Document添加Field
			File f=new File("f:/lucene/lucenes");
			
			   for(File file:f.listFiles())
			   {
				   document=new Document();
				   document.add(new Field("content",new FileReader(file)));
				   document.add(new Field("filename",file.getName(),Field.Store.YES,
						   Field.Index.NOT_ANALYZED));
				   document.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,
						   Field.Index.NOT_ANALYZED));
			   }
		 //5.通过IndexWriter添加文档到索引中
			   writer.addDocument(document);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(writer!=null)
					writer.close();
				 
			    }catch (CorruptIndexException e2) {
					e2.printStackTrace();
					// TODO: handle exception
				}catch (IOException e2) {
					e2.printStackTrace();
					// TODO: handle exception
				}
		}
		
	
		
	}
}
//测试类

package org.se.lucene;

import org.junit.Test;

public class LuceneTest {
	@Test
	public void test_Index()
	{
		hellowLucene hLucene=new hellowLucene();
		hLucene.index();
	}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics