Lucene学习入门3

前面对索引库做的增删改查不能在同一个索引库里同时进行运行多个索引,下面对前面的代码进行一下修改,来解决这个问题。

首先、先写个工具类,初始化IndexWriter和关闭IndexWriter

package com.lucene.luceneutil;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriter.MaxFieldLength;

public class LuceneUtils {

private static IndexWriter indexWriter ;

static{

//初始化IndexWriter

try {

indexWriter = new IndexWriter(Configuration.getDirectory(), Configuration.getAnalyzer(), MaxFieldLength.LIMITED);

System.out.println("已经初始化了IndexWriter!");

// 在JVM退出前要执行代码

Runtime.getRuntime().addShutdownHook(new Thread(){

public void run(){

try {

indexWriter.close();

System.out.println("已经关闭了IndexWriter");

} catch (Exception e) {

throw new RuntimeException(e);

}

}

});

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static IndexWriter getIndexWriter() {

return indexWriter;

}

public static void setIndexWriter(IndexWriter indexWriter) {

LuceneUtils.indexWriter = indexWriter;

}

}
 
然后,把实现类的用new方法直接创建IndexWriter对象的全部替换掉,例如:
package com.lucene.indexdao.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.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.util.Version;
import com.lucene.entity.ArticleEntity;
import com.lucene.entity.QueryResult;
import com.lucene.indexdao.ArticleEntityIndexDao;
import com.lucene.luceneutil.ArticleDocumentUtils;
import com.lucene.luceneutil.Configuration;
import com.lucene.luceneutil.LuceneUtils;
public class ArticleEntityIndexDaoImpl implements ArticleEntityIndexDao {
@Override
public void save(ArticleEntity article) {
// 1,把Article转为Document
Document doc = ArticleDocumentUtils.Article2Document(article);
// 2,把Document存到索引库中
try {
LuceneUtils.getIndexWriter().addDocument(doc);// 建立索引
LuceneUtils.getIndexWriter().commit();//提交更改
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Term:就是某Field中的某个关键词。
*/
@Override
public void delete(Integer id) {
try {
Term term = new Term("id", id.toString());
LuceneUtils.getIndexWriter().deleteDocuments(term); // 删除包含指定Term的索引数据
LuceneUtils.getIndexWriter().commit();   //提交更改
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 更新就是先删除再添加
*/
@Override
public void update(ArticleEntity article) {
try {
Term term = new Term("id", article.toString());
Document doc = ArticleDocumentUtils.Article2Document(article);
LuceneUtils.getIndexWriter().updateDocument(term, doc);//更新索引
LuceneUtils.getIndexWriter().commit();   //提交更改
// indexWrite.deleteDocuments(term);
// indexWrite.addDocument(doc);
} catch (Exception e) {
throw new RuntimeException(e);
} 
}
@Override
public QueryResult<ArticleEntity> search(String query, int firstResult,
int number) {
IndexSearcher searchQuery = null;
QueryParser queryParser = null;
Query queryStr;
try {
// queryParser = new QueryParser(Version.LUCENE_30, "title",
// Configuration.getAnalyzer());这是默认在title里搜索
queryParser = new MultiFieldQueryParser(Version.LUCENE_30,new String[] { "title", "content" },Configuration.getAnalyzer());// 多字段中搜索
queryStr = queryParser.parse(query);
searchQuery = new IndexSearcher(Configuration.getDirectory());
TopDocs topDocs = searchQuery.search(queryStr, 1000);
int totalNum = topDocs.totalHits;// 符合条件的总记录数
ScoreDoc[] scoredoc = topDocs.scoreDocs;// 符合条件的前n条信息
// 处理结果
List<ArticleEntity> list = new ArrayList<ArticleEntity>();
int endLenght = Math.max(firstResult + number, scoredoc.length);
for (int i = firstResult; i < endLenght; i++) {
Document doc = searchQuery.doc(scoredoc[i].doc); // 得到Document对象
ArticleEntity e = ArticleDocumentUtils.Document2Article(doc); // 得到ArticleEntity对象
list.add(e);
}
return new QueryResult<ArticleEntity>(totalNum, list);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
searchQuery.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}

 

这样,在程序加载的时候就初始化了IndexWriter对象,在JVM退出之前关闭IndexWriter对象,这样就不会出现死锁的问题了。

 

你可能感兴趣的:(Lucene,全文搜索,多个indexWriter索引)