Lucene全文检索中用到方法
程序员文章站
2022-03-04 23:42:05
...
Services中调用接口方法:
1.重新生成索引
/**
* 重新生成索引
*
* @return
*/
public String newIndex(String page) {
HelpSearchDomain helpSearchDomain = (HelpSearchDomain) getContextBean("helpSearchDomain");
BaseInfoAttributeDomain baseInfoAttributeDomain = (BaseInfoAttributeDomain) getContextBean("baseInfoAttributeDomain");
//设置分页对象信息
SplitPage sp = new SplitPage();
sp.setCurrentPage(Integer.parseInt(page));
sp.setPageSize(20);
List knowledgeList = baseInfoAttributeDomain.getAllBaseInfoAttribute("1", sp);
String path = "D:\\upload";
path = PropertiesUtil.get("PATH", path);
if (knowledgeList != null && knowledgeList.size() > 0) {
for (Iterator iterator = knowledgeList.iterator(); iterator.hasNext();) {
BaseInfoAttribute bi = (BaseInfoAttribute) iterator.next();
BaseInfoAttribute bia = null;
System.out.println(bi.getInfoType().getType());
if (bi.getInfoType().getType().equals(InfoType.ZSK)) {
bia = baseInfoAttributeDomain.findBaseInfoAttribute(bi.getId());
}
try {
helpSearchDomain.delKnowledgeIndex(path + "\\" + bia.getContent(), bia.getInfoType().getName(), path);//删除索引
} catch (Exception e) {
e.getMessage();
}
}
for (Iterator iterator = knowledgeList.iterator(); iterator.hasNext();) {
BaseInfoAttribute bi = (BaseInfoAttribute) iterator.next();
if (bi.getInfoType().getType().equals(InfoType.ZSK)) {
BaseInfoAttribute bia = baseInfoAttributeDomain.findBaseInfoAttribute(bi.getId());
helpSearchDomain.createKnowledgeIndex(false, path + "\\" + bia.getContent(), path, bia.getTitle(), bia.getCreatetime(), bia.getInfoType().getName(), String.valueOf(bi.getId()));
}
}
}
return "索引生成成功";
}
2.根据条件检索内容
/**
* 根据条件检索内容
*
* @param keyword
* @param type
* @param page
* @return
*/
public Object searchKnowledgeQuery(String keyword, String type, String page) {
HelpSearchDomain helpSearchDomain = (HelpSearchDomain) getContextBean("helpSearchDomain");
String path = "D:\\upload";
SplitPage sp = new SplitPage();
sp.setPageSize(18);
sp.setCurrentPage(page == null ? 1 : Integer.parseInt(page));
String isExist = helpSearchDomain.isExistsKnowlegeIndex(path);
List returnlist = new ArrayList();
List _list = new ArrayList();
List list = new ArrayList();
if (!isExist.equals("")) {
if (type.equals("3")) {//标题
list = helpSearchDomain.searchKnowlegeByKey("title", keyword, path, sp);
if (list != null && list.size() > 0) {
int len = list.size();
for (int i = 0; i < len; i++) {
Map m = (Map) list.get(i);
Map map = new HashMap();
map.put("id", m.get("knowlegeid"));
map.put("title", "<a href=\"javascript:void(0)\" onclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
map.put("infoType", m.get("infoType"));
map.put("createtime", m.get("createtime"));
_list.add(map);
}
}
} else if (type.equals("2")) {// 内容
list = helpSearchDomain.searchKnowlegeByKey("contents", keyword, path, sp);
if (list != null && list.size() > 0) {
int len = list.size();
for (int i = 0; i < len; i++) {
Map m = (Map) list.get(i);
Map map = new HashMap();
map.put("id", m.get("knowlegeid"));
map.put("title", "<a href=\"javascript:void(0)\" onclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
map.put("infoType", m.get("infoType"));
map.put("createtime", m.get("createtime"));
_list.add(map);
}
}
} else { //全文
list = helpSearchDomain.searchKnowlegeByKey("all", keyword, path, sp);
if (list != null && list.size() > 0) {
int len = list.size();
for (int i = 0; i < len; i++) {
Map m = (Map) list.get(i);
Map map = new HashMap();
map.put("id", m.get("knowlegeid"));
map.put("title", "<a href=\"javascript:void(0)\" onclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
map.put("infoType", m.get("infoType"));
map.put("createtime", m.get("createtime"));
_list.add(map);
}
}
}
returnlist.add(_list);
returnlist.add(sp);
return returnlist;
} else {
return "";
}
}
有不懂得地方加:QQ413540158
推荐阅读
-
mysql 全文检索中文解决方法及实例代码
-
Django中使用Whoosh进行全文检索的方法
-
SQL Server 2005 中做全文检索的方法分享
-
Mybaits 源码解析 (六)----- 全网最详细:Select 语句的执行过程分析(上篇)(Mapper方法是如何调用到XML中的SQL的?)
-
Net Core使用Lucene.Net和盘古分词器 实现全文检索
-
干货 |《从Lucene到Elasticsearch全文检索实战》拆解实践
-
《从Lucene到Elasticsearch:全文检索实战》学习笔记三
-
全文检索-Lucene.net
-
快速学习方法:如何将碎片化的知识应用到工作中?
-
全文检索---lucene学习笔记 有案例