public class DBIndex{ public static final config _$=new config(); public static class config{ public static final Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_47);//分词器 public String CLASS_PATH= Config.CLASS_PATH; public config(){ } public String getDatePath() { return CLASS_PATH+"query/index"; } public File getDataFile(){ return new File(getDatePath()); } public String getIndexPath() { return CLASS_PATH+"query/index"; } public File getIndexFile(){ return new File(getIndexPath()); } /** * 将字符串中HTML标记清空 * @param msg * @return String */ public String clearHTMLToString(String msg){ if(StringUtils.isEmpty(msg)){ return ""; } return msg.replaceAll("(?is)<(.*?)>","").replaceAll("\\s*|\t|\r|\n",""); } /**将查询出的Map对象,转换为Lucene中的Document对象。 * @param news * @return org.apache.lucene.document.Document * */ public void toDocument(IndexWriter iw,Map<String,Object> news)throws Exception{ Document doc = new Document(); Iterator iter = news.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val=entry.getValue()==null?"":entry.getValue(); if("FILE_NAME".equals(key.toString().toUpperCase())){ List file=UpInfoService.toMapList(String.valueOf(val)); String textString=""; for(int i=0;i<file.size();i++){ Map map=(Map)file.get(i); textString+=map.get("description").toString(); } val=textString; }else if(val instanceof Date){ doc.add(new StringField(key.toString(),WebUtil.getDate((Date)val,"yyyy-MM-dd HH:mm:ss"), Field.Store.YES));//标题 }else{ doc.add(new StringField(key.toString(),clearHTMLToString(String.valueOf(val)), Field.Store.YES)); } } iw.addDocument(doc); } /** * 读取文件内容为String * @param file * @return String */ public String readFileContent(File file) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); StringBuffer content = new StringBuffer(); for (String line = null; (line = reader.readLine()) != null;) { content.append(line).append("\n"); } return content.toString(); } catch (Exception e) { throw new RuntimeException(e); } } public boolean isEmpty(Object obj){ if(obj==null){ return true; } return "".equals(obj.toString()); } public boolean is(String filed,String ...filds){ for (String string : filds) { if(filed.equalsIgnoreCase(string)){ return true; } } return false; } }
相关推荐
《Lucene4.7在Web应用中的实践:结合SpringMVC与MyBatis3》 在信息化时代,搜索引擎已经成为我们日常获取信息的重要工具。Apache Lucene作为一款强大的全文搜索引擎库,为开发者提供了丰富的功能,帮助构建高效、可...
Lucene 是一款强大的全文搜索引擎库,广泛应用于各种信息检索系统中。在本实例中,我们将深入探讨Lucene 4.7版本,涵盖索引的创建、修改、删除,以及查询时的排序、分页、优化和高亮显示等功能。此外,我们还将了解...
IKAnalyzer2012FF 是一个针对中文处理的开源分词器,特别适合于做搜索引擎或者信息检索相关的应用。它基于前缀词典的动态扩展词典方式,能够较好地解决新词识别问题。IKAnalyzer对Lucene的集成使得开发者能够在...
Lucene是一个由Apache软件基金会开发的开源全文检索库,它为Java开发者提供了强大的文本搜索功能。在4.7.0这个版本中,Lucene不仅强化了其核心搜索功能,还对高亮显示进行了优化,使得用户能够更直观地找到搜索结果...
《Lucene 4.7.0官方文档》是开发者们深入了解和使用Apache Lucene库的重要参考资料,这是一款广泛应用于全文检索、信息检索领域的开源Java库。Lucene提供了强大的文本分析和索引功能,使得开发者可以轻松地在大量...
#### 八、集成LUCENE全文搜索引擎 **8.1 创建搜索索引** - **8.1.1** 安装Lucene搜索引擎; - **8.1.2** 创建索引文件。 **8.2 搜索引擎配置-概述** - 学习如何配置Lucene搜索引擎的基本设置。 **8.3 搜索引擎...