lucene 搜索列表显示 lucene-core-3.0.0.jar

/**
* @se 搜索列表显示
* @param paginator
* @param initData
* @return List
* @throws Exception
*/
@SuppressWarnings("unchecked")
public List searchEppendActiveList(Pagination paginator, Map initData)
throws Exception {
IndexSearcher indexSearcher = LuceneUtils
.getEmpuserbaseSearcher(LuceneUtils
.getEmpuserActivityPathDirectory());
if (null == indexSearcher)
return null;
BooleanQuery booleanQuery = new BooleanQuery();
Query query = null;
int firstType = Integer.parseInt(initData.get("type").toString());
if (firstType > 0) {
query = new TermQuery(new Term(Constants.TYPE, String
.valueOf(firstType)));
booleanQuery.add(query, BooleanClause.Occur.MUST);
}
// 数据标志
query = new TermQuery(new Term(Constants.TOTALDATA, String.valueOf(1)));
booleanQuery.add(query, BooleanClause.Occur.MUST);
// 排序
Sort sort = new Sort();
ScoreDoc[] hits = indexSearcher.search(booleanQuery, null, 1000, sort).scoreDocs;
int score = null == hits ? 0 : hits.length;
paginator.setTotalCount(score);
return eppendActiveDataList(hits, paginator, indexSearcher);
}

/**
* @see 返回数据list封装
* @param hits
* @param paginator
* @param queryKey
* @return list
*/
@SuppressWarnings("unchecked")
private List eppendActiveDataList(ScoreDoc[] hits, Pagination paginator,
IndexSearcher indexSearcher) throws Exception {
List objList = new ArrayList();

for (int i = (paginator.getPageNo() - 1) * paginator.getRowsPerPage(); i < (paginator
.getPageNo())
* paginator.getRowsPerPage()
&& i < hits.length; i++) {
Document doc = indexSearcher.doc(hits[i].doc);
Object[] obj = new Object[7];
// 活动id
obj[0] = doc.get(Constants.ID);
// 活动标题
obj[1] = doc.get(Constants.USERCODE);
// 日期
obj[2] = doc.get(Constants.NICKNAME);

objList.add(obj);
}

return objList;
}


package test;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.SimpleFSDirectory;

public class LuceneUtils {

private static String eppendActivityPath = "D:/index/empuserbase";
private static Directory empuserActivityPathDirectory;
public static Directory getEmpuserActivityPathDirectory() {
if (null == empuserActivityPathDirectory)
setEmpuserActivityPathDirectory(readDirectory(
empuserActivityPathDirectory, eppendActivityPath));
return empuserActivityPathDirectory;
}
public static void setEmpuserActivityPathDirectory(
Directory empuserActivityPathDirectory) {
LuceneUtils.empuserActivityPathDirectory = empuserActivityPathDirectory;
}

/**
* @see 读取文件目录,首先尝试建立内存索引文件,不成功则从硬盘读取
*/
private static Directory readDirectory(Directory dest, String path) {
System.out.println("目录:" + dest);
if (dest != null) {
return dest;
} else {
try {
dest = new RAMDirectory(new SimpleFSDirectory(new File(path)));
System.out.println("将" + path + "读入内存,当前时间:" + new Date());
} catch (IOException e) {
e.printStackTrace();
try {
dest = FSDirectory.open(new File(path));
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
}
return dest;
}
}

private volatile static IndexSearcher empuserbaseSearcher;

public synchronized static IndexSearcher getEmpuserbaseSearcher(
Directory directory) {
if (empuserbaseSearcher == null) {
try {
System.out.println("新建empuserbaseSearcher实例");
synchronized (eppendActivityPath) {
if (empuserbaseSearcher == null) {
empuserbaseSearcher = new IndexSearcher(directory);
}
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return empuserbaseSearcher;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值