lucene4.3—索引的建立

很早就对lucene这个基于java的apache开源全文搜索框架有过初步的学习了,但是由于学习等各种原因,迟迟都没有发表一些博文,今天就贴出一些博主的一些学习笔记,希望对lucene有兴趣的博友指点指点。

对于学习lucene这个全文搜索框架,第一步,要学会建立起索引:

package lucene;

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

public class Lucene {

	/**
	 * 建立索引
	 * @throws IOException 
	 */
	
	public void index() throws IOException 
	{
		//1、创建directory
		Directory directory=new RAMDirectory(); //创建内存索引
				
		Directory directory2=FSDirectory.open(new File("E:/lucene/index01")); //索引创建于硬盘中
				
		//2、创建indexWriter
				
		//创建IndexWriterConfig,从中文名可以看出,这个是IndexWriter的配置类, 有两个参数,第一个是版本号,第二个是分词器
		IndexWriterConfig iwc =new IndexWriterConfig(Version.LUCENE_43,new StandardAnalyzer(Version.LUCENE_43));
				
		IndexWriter iw=new IndexWriter(directory2,iwc);
				
		//3、创建document对象
		
		Document document;
				
		//4、为document对象添加Field域
				
		//创建文件夹,文件夹里面有三个文档
		File f=new File("E:/lucene/luceneexamplefile");
				
		//遍历文件夹里面的三个文档,为每个文档添加索引域,即建立当前这篇文档的索引
		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("filepath",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
			
			
			//5、通过indexWriter将文档添加到索引中
			
			//因为iw是indexWriter,它里面封装了索引创建的目标地方和分词器等等的配置,
			//而且提供了一个封装方法,只要调用这个方法,把这篇文档传进去,即可建立起当前文档的索引
			iw.addDocument(document);
			
		}
				
		//类似于数据库一样,用完要关闭
		iw.close();
		
	}
	
}

   上面代码是建立索引的步骤以及代码实现,下面写个测试类:

public class LuceneTest {

	
	@Test
	public void testIndex() throws IOException
	{
		
		Lucene lucene=new Lucene();
		
		lucene.index();
	}
	
}

这样就可以生成索引了,执行结果如下:


 就这样,索引就建立成功了。

猜你喜欢

转载自zhh9106.iteye.com/blog/2035732