提供一个可用的 lucene 增删改查工具类
2017-12-20 16:50
274 查看
lucene 增删改查工具类
package com.ljq.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
/**
* lucene工具类,采用IKAnalyzer中文分词器
*
* @author
* @version
*/
public class LuceneUtil {
/** 索引库路径 */
private static final String indexPath = XMLPropertyConfig.getConfigXML()
.getString("index_path");
public static IndexWriter indexWriter = null;
private static final Logger log=Logger.getLogger(LuceneUtil.class);
public static IndexWriter getIndexWriter(){
if(indexWriter == null){
try {
//索引库路径不存在则新建一个
File indexFile=new File(indexPath);
if(!indexFile.exists()) indexFile.mkdir();
Directory fsDirectory = FSDirectory.open(indexFile);
IndexWriterConfig confIndex = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer());
confIndex.setOpenMode(OpenMode.CREATE_OR_APPEND);
if (IndexWriter.isLocked(fsDirectory)) {
IndexWriter.unlock(fsDirectory);
}
indexWriter =new IndexWriter(fsDirectory, confIndex);
} catch (Exception e) {
e.printStackTrace();
}
}
return indexWriter;
}
/**
* 创建索引
*
* @param doc
* @throws Exception
*/
public static boolean createIndex(Document doc) {
List<Document> docs = new ArrayList<Document>();
docs.add(doc);
return createIndex(docs);
}
/**
* 创建索引
*
* @param docs
* @throws Exception
*/
public static boolean createIndex(List<Document> docs) {
try {
for (Document doc : docs) {
getIndexWriter().addDocument(doc);
}
// 优化操作
getIndexWriter().commit();
getIndexWriter().forceMerge(1); // forceMerge代替optimize
log.info("lucene create success.");
return true;
} catch (Exception e) {
log.error("lucene create failure.", e);
return false;
} finally {
if (getIndexWriter() != null) {
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 更新索引
*
* 例如:Term term = new Term("id","1234567");
* 先去索引文件里查找id为1234567的Document,如果有就更新它(如果有多条,最后更新后只有一条),如果没有就新增。
* 数据库更新的时候,我们可以只针对某个列来更新,而lucene只能针对一行数据更新。
*
* @param field Document的Field(类似数据库的字段)
* @param value Field中的一个关键词
* @param doc
* @return
*/
public static boolean updateIndex(String field, String value, Document doc) {
try {
getIndexWriter().updateDocument(new Term(field, value), doc);
log.info("lucene update success.");
return true;
} catch (Exception e) {
log.error("lucene update failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除索引
*
* @param field Document的Field(类似数据库的字段)
* @param value Field中的一个关键词
* @param doc
* @return
*/
public static boolean deleteIndex(String field, String value) {
try {
getIndexWriter().deleteDocuments(new Term(field, value));
log.info("lucene delete success.");
return true;
} catch (Exception e) {
log.error("lucene delete failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除整个索引库
*
* @return
*/
public static boolean deleteAllIndex() {
try {
getIndexWriter().deleteAll();
log.info("lucene delete all success.");
return true;
} catch (Exception e) {
log.error("lucene delete all failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 判断索引库是否已创建
*
* @return true:存在,false:不存在
* @throws Exception
*/
public static boolean existsIndex() throws Exception {
File file = new File(indexPath);
if (!file.exists()) {
file.mkdirs();
}
String indexSufix = "/segments.gen";
// 根据索引文件segments.gen是否存在判断是否是第一次创建索引
File indexFile = new File(indexPath + indexSufix);
return indexFile.exists();
}
}
package com.ljq.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
/**
* lucene工具类,采用IKAnalyzer中文分词器
*
* @author
* @version
*/
public class LuceneUtil {
/** 索引库路径 */
private static final String indexPath = XMLPropertyConfig.getConfigXML()
.getString("index_path");
public static IndexWriter indexWriter = null;
private static final Logger log=Logger.getLogger(LuceneUtil.class);
public static IndexWriter getIndexWriter(){
if(indexWriter == null){
try {
//索引库路径不存在则新建一个
File indexFile=new File(indexPath);
if(!indexFile.exists()) indexFile.mkdir();
Directory fsDirectory = FSDirectory.open(indexFile);
IndexWriterConfig confIndex = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer());
confIndex.setOpenMode(OpenMode.CREATE_OR_APPEND);
if (IndexWriter.isLocked(fsDirectory)) {
IndexWriter.unlock(fsDirectory);
}
indexWriter =new IndexWriter(fsDirectory, confIndex);
} catch (Exception e) {
e.printStackTrace();
}
}
return indexWriter;
}
/**
* 创建索引
*
* @param doc
* @throws Exception
*/
public static boolean createIndex(Document doc) {
List<Document> docs = new ArrayList<Document>();
docs.add(doc);
return createIndex(docs);
}
/**
* 创建索引
*
* @param docs
* @throws Exception
*/
public static boolean createIndex(List<Document> docs) {
try {
for (Document doc : docs) {
getIndexWriter().addDocument(doc);
}
// 优化操作
getIndexWriter().commit();
getIndexWriter().forceMerge(1); // forceMerge代替optimize
log.info("lucene create success.");
return true;
} catch (Exception e) {
log.error("lucene create failure.", e);
return false;
} finally {
if (getIndexWriter() != null) {
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 更新索引
*
* 例如:Term term = new Term("id","1234567");
* 先去索引文件里查找id为1234567的Document,如果有就更新它(如果有多条,最后更新后只有一条),如果没有就新增。
* 数据库更新的时候,我们可以只针对某个列来更新,而lucene只能针对一行数据更新。
*
* @param field Document的Field(类似数据库的字段)
* @param value Field中的一个关键词
* @param doc
* @return
*/
public static boolean updateIndex(String field, String value, Document doc) {
try {
getIndexWriter().updateDocument(new Term(field, value), doc);
log.info("lucene update success.");
return true;
} catch (Exception e) {
log.error("lucene update failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除索引
*
* @param field Document的Field(类似数据库的字段)
* @param value Field中的一个关键词
* @param doc
* @return
*/
public static boolean deleteIndex(String field, String value) {
try {
getIndexWriter().deleteDocuments(new Term(field, value));
log.info("lucene delete success.");
return true;
} catch (Exception e) {
log.error("lucene delete failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除整个索引库
*
* @return
*/
public static boolean deleteAllIndex() {
try {
getIndexWriter().deleteAll();
log.info("lucene delete all success.");
return true;
} catch (Exception e) {
log.error("lucene delete all failure.", e);
return false;
}finally{
if(getIndexWriter()!=null){
try {
getIndexWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 判断索引库是否已创建
*
* @return true:存在,false:不存在
* @throws Exception
*/
public static boolean existsIndex() throws Exception {
File file = new File(indexPath);
if (!file.exists()) {
file.mkdirs();
}
String indexSufix = "/segments.gen";
// 根据索引文件segments.gen是否存在判断是否是第一次创建索引
File indexFile = new File(indexPath + indexSufix);
return indexFile.exists();
}
}
相关文章推荐
- 给Android项目中的工具类提供一个上下文对象
- 一个java操作ini格式文件的工具类,支持增删查改,不过由于项目所需,只做了针对单行的操作。写得不好求指教。
- Android SQLite 一个工具类搞定基本的数据增删改查
- 提供一个可用的RTSP地址
- weixin4j开发—为大家提供一个获取Weixin对象的工具类
- 用Java提供的工具类对一个字符串进行加密
- 提供一个win7可用的sourceInsight地址
- weixin4j开发—为大家提供一个获取Weixin对象的工具类
- 封装JDBC的一个通用增删改查工具类,将数据集rs转为list
- 一个Lucene4.6工具类
- 提供一个AES128位/CBC模式加解密工具类
- 提供一套modal一个webView控制器的工具类
- 使用配置文件为购物车提供一个商品放置数据库
- 九宫格 Swift提供经典的数组和字典两种集合类型来存储集合数据,使用数组实现一个九宫格程序,
- 彻底解决:Apache启动问题—(OS 10022)提供了一个无效的参数。
- OpenStack 是一个旨在为公共及私有云的建设与管理提供软件...
- 【No10.】一个超便捷的Log工具类
- Azkaban3X的编译安装与使用(确定可用)--提供已经编译好的工具供大家使用
- 如何在Kubernetes中部署一个高可用的PostgreSQL集群环境
- corosync+pacemaker+nfs提供mysql高可用