众所周知,lucene是一门很重要的技术,在茫茫技术大海中有举足轻重的作用,在搜索方面功不可没。至于,还在疑问lucene是什么、有神马用处的童鞋,建议去google一下,你会找到大量的描述,在此我就不多说了。我个人之前也自学过lucene,但那个时候没学透,只是简单了解了下,能建索引和搜索而已,最近想换个搞搜索的工作,所以,想重新再学习下。以前我学的是lucene2.0,现在我学的lucene3.5,版本差别很大很大很大。。。不过,慢慢学就会好的。
lucene,慢慢学,不懂的就google。
废话不多说了,马上记录下来吧,从建立索引开始。
建立索引:用lucene开发搜索的模块,需要先进行建立索引的操作,因为搜索的时候是去搜索索引,这样会大大提高搜索效率,所以需要先建立索引,至于怎么建呢,往下看吧。
用到的lucene中的类,Directory、IndexWriterConfig、IndexWriter、Document
怎么样,不多吧,就4个对象,导包的时候,记得不要导错,全都是lucen的类。
以下就是建立索引的步骤:
1、创建Directory对象,即创建目录对象,一个是索引的保存位置
2、创建IndexWriterConfig对象,这是来配置下一步中IndexWriter对象的,在IndexWriterConfig中需要配置所使用lucene的版本和所使用的分词器。
3、创建IndexWriter对象
4、创建Document对象,因为在lucene中,被索引的内容是被抽象成一个个的Document对象的,因而Document对象比不可少。
5、为Document对象写入具体的被索引内容Field
6、通过IndexWriter,把Document添加到索引中
7、关闭IndexWriter对象
以下是代码演示:
package com.hlp.lucene.createIndex;
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.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.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
* 功能:建立索引
*
*
*/
public class CreateIndex1
{
// 数据目录,即对该目录下的文件建立索引,为简单起见,本目录下存放的都是txt的文件
String luceneData = "G://lucene//luceneData";
// 存放索引的目录
String luceneIndex = "G://lucene//luceneIndex1";
// 分词器,lucene自带的默认分词器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
public void createIndex()
{
IndexWriter indexWriter = null;
try
{
// 1、创建Directory目录,声明索引存放的位置
// 1.1、把索引存放在内存中
//Directory directory = new RAMDirectory();
// 1.2、把索引存放在硬盘上的指定位置
Directory directory = FSDirectory.open(new File(luceneIndex));
// 2、创建IIndexWriterConfig对象,来配置IndexWriter
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
// 3、创建IndexWriter对象
indexWriter = new IndexWriter(directory, iwConfig);
// 4、创建Document对象
Document document = null;
// 5、为Document对象添加Field
File fileFolderFile = new File(luceneData);
File fileList[] = fileFolderFile.listFiles();
File fileTemp = null;
for(int i = 0; i < fileList.length; i++)
{
fileTemp = fileList[i];
document = new Document();
document.add(new Field("fileContent", new FileReader(fileTemp)));
document.add(new Field("fileName", fileTemp.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("filePath", fileTemp.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
// 6、通过IndexWriter,把Document添加到索引中
indexWriter.addDocument(document);
}
}
catch (CorruptIndexException e)
{
e.printStackTrace();
}
catch (LockObtainFailedException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}finally
{
// 7、关闭IndexWriter
if(null != indexWriter)
{
try
{
indexWriter.close();
}
catch (CorruptIndexException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
} // end finally
} // end createIndex
}
自己再写一个测试方法测试一下,就可以产生索引了,下图是产生的索引文件。