lucene4.7(3) 全文检索之 相关类

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;
		} 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值