建索引:
使用的对象和基本步骤:
Analyzer,解析器。
IndexWriter,需要对象IndexWriter来进行索引的创建与更新。
Document,写入的文档,是IndexWriter的基本对象。(一条报警可以用一个文档表示)
Field,一个Document可以有多个Field,这是我们存储的基本单位。(PCIP等都可以视为Field)注:field默认域名区分大小写,最好统一。
A. 创建写对象IndexWriter,它依赖于Analyzer、存储路径,可通过IndexWriterConfig对其进行参数设置。
B. 创建空文档Document doc = newDocument();
C. 向空文档里面添加若干个Field,doc.add(new Field("PCIP", fields[0],Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
注:
Field参数STORE,与索引无关,是否额外存储原文 ,可以在搜索结果后调用出来,
NO不额外存储;
YES,额外存储。
Field参数INDEX,
NO,不索引;
ANALYZED,分词后索引;
ANALYZED_NO_NORMS,分词索引,不存储NORMS;
NOT_ANALYZED_NO_NORMS,不分词,索引,不存储NORMS。
除了NO外都算索引,可以搜索。NORMS存储了boost所需信息,包含了NORM可能会占用更多内存。
D. 向IndexWriter添加Document,writer.addDocument(doc);
E. 优化索引(优化相对比较慢,可以选择进行,优化之后可以达到最大查询速度,//writer.optimize();//优化索引
importorg.apache.lucene.analysis.Analyzer;
importorg.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
importorg.apache.lucene.index.CorruptIndexException;
importorg.apache.lucene.index.IndexWriter;
importorg.apache.lucene.index.IndexWriterConfig;
importorg.apache.lucene.index.Term;
importorg.apache.lucene.store.Directory;
importorg.apache.lucene.store.FSDirectory;
importorg.apache.lucene.store.LockObtainFailedException;
importorg.apache.lucene.util.Version;我使用当中主要发生的变化已经用红色标记出
@SuppressWarnings("deprecation")
public void BuildLawyerPublic(ResponseList<SearchLawyer> lawyerList,
String path, boolean overwrite, Date start, Date end) {
IndexWriter indexWriter = null;
try {
try {
Analyzer analyzer = new IKAnalyzer();//分词类变为新的
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_33, analyzer);//indexWrite配置新的
Directory dir =FSDirectory.open(new File(path)) ;//地址
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);//总是重新创建
indexWriter = new IndexWriter(dir,indexWriterConfig);//新的方法
} catch (IOException e) {
e.printStackTrace();
this.BuildAll();
return;
}
long count = 0;
long startTime = new Date().getTime();
for (int i = 0; i < lawyerList.size(); i++) {
// 按小时或天建立索弄1�7
if (path == hoursDir || path == dailyDir) {
SearchLawyer lawyerBean = new SearchLawyer();
BeanUtils.copyProperties(lawyerList.get(i), lawyerBean);
// 更新索引
if (((SearchLawyer) lawyerList.get(i)).getJoinDate()
.before(start)) {
Term term = new Term("consultID", String
.valueOf(((SearchLawyer) lawyerList.get(i))
.getLawyerID()));
indexWriter.updateDocument(term,
convert2Doc(lawyerBean));
update_count++;
} else {// 新增索引
indexWriter.addDocument(convert2Doc(lawyerBean));
add_count++;
}
} else {
// 第一次建立索弄1�7
SearchLawyer lawyerBean = (SearchLawyer) lawyerList.get(i);
if (lawyerBean != null) {
Document hdoc = convert2Doc(lawyerBean);
indexWriter.addDocument(hdoc);
}
count++;
}
}
indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();
logger.debug("It takes " + (endTime - startTime)
+ "ms index count :" + count
+ " consultpublic count:==============="
+ lawyerList.size());
} catch(CorruptIndexException e) {
logger.error("{}", e);
e.printStackTrace();
} catch(LockObtainFailedException e) {
logger.error("{}", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("{}", e);
e.printStackTrace();
}
}
//bean 2
public Document convert2Doc(SearchLawyer lawyer) {
Document doc = new Document();
try {
// 律师主键
doc.add(new Field("lawyerID", String.valueOf(lawyer.getLawyerID()),
Field.Store.YES, Field.Index.NO));//NO,不索引$1�7
// 律师姓名
doc.add(new Field("fullName", String.valueOf(lawyer.getFullName()),
Field.Store.YES, Field.Index.ANALYZED));//ANALYZED,分词后索引
// 律师电话
doc.add(new Field("phone", String.valueOf(lawyer.getPhone()),
Field.Store.YES, Field.Index.NO));
// 律师头像
doc.add(new Field("photo", String.valueOf(lawyer
.getPhoto()), Field.Store.YES, Field.Index.NO));
// 律师箄1�7仄1�7
doc.add(new Field("lawyer_Intro", String.valueOf(lawyer
.getLawyer_Intro()), Field.Store.YES,
Field.Index.ANALYZED));
// 律所名称
doc.add(new Field("officeName", String
.valueOf(lawyer.getOfficeName()), Field.Store.YES,
Field.Index.NOT_ANALYZED));//NOT_ANALYZED,不分词索引
// 律师扄1�7在省
doc.add(new Field("provinceName", String.valueOf(lawyer
.getProvinceName()), Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 律师扄1�7在市
doc.add(new Field("cityName", String.valueOf(lawyer.getCityName()),
Field.Store.YES, Field.Index.NOT_ANALYZED));
// 专注类别
doc.add(new Field("specialtyName", String
.valueOf(lawyer.getSpecialtyName()), Field.Store.YES,
Field.Index.ANALYZED));
// 律师添加的时闄1�7
doc.add(new Field("joinDate", DateUtil.getDateTime(
"yyyy-MM-dd hh:mm:ss", lawyer.getJoinDate()),
Field.Store.YES, Field.Index.NO));
} catch (Exception e) {
logger.error("{}", e);
e.printStackTrace();
}
return doc;
}