• SolrJ解析MoreLikeThis查询结果


    直接上代码:

    /**
    	 * 解析MoreLikeThis
    	 * @author  likehua
    	 * */
    	public List<SolrDocument>  parseMoreLikeThis(QueryResponse response){
    		List<SolrDocument> lst=new ArrayList<SolrDocument>();
    		SimpleOrderedMap res= (SimpleOrderedMap) response.getResponse().get("moreLikeThis");
    		for(int i=0;i<res.size();i++){
    			Object o=res.getVal(i);
    			if(o instanceof SolrDocumentList){
    				SolrDocumentList items=(SolrDocumentList) o;
    				for(SolrDocument d:items){
    					if(d!=null){
    						lst.add(d);
    					}
    				}
    			}
    		}
    		return lst;
    	}
    	/**
    	 * MoreLikeThis查询
    	 * @author  likehua
    	 * */
    	public  List<Index> getLikeResult(String param){
    		List<Index> likes = new ArrayList<Index>();
    		String strQuery = "";
    		if(param != null && param.length()>0){
    			Pattern pt = Pattern.compile("\\s+");
    			Matcher mc = pt.matcher(param.trim());
    			String strParam = mc.replaceAll(","); 
    			strQuery = "geo_name:"+strParam+ " OR geo_summary:"+strParam;		
        	}else{
        		strQuery = "*:*";
        	}
    			
    		try {
    			SolrQuery likeQuery = new SolrQuery(strQuery);
    			likeQuery.setParam("mlt", "true").setParam("mlt.fl", "geo_name,geo_summary").setParam("mlt.count", "1");						
    			QueryResponse response = SolrServer.getServer().query(likeQuery);
    	    	List<SolrDocument> responseList=parseMoreLikeThis(response);
    			//SimpleOrderedMap res=  (SimpleOrderedMap) response.getResponse().get("moreLikeThis");
    			for(SolrDocument sd:responseList) {
    				String name = (String)sd.getFieldValue("geo_name");
    				Index index = new Index();
    				index.setLike(name);
    				likes.add(index);
    			}
    		} catch (SolrServerException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}	
    		return likes;		
    	}
    
  • 相关阅读:
    第十九周作业
    第十八周作业
    第十七周作业
    第16周作业
    第15周作业
    第14周作业作业
    第13周作业
    第12周作业
    第二阶段月考
    RDB和AOF的区别
  • 原文地址:https://www.cnblogs.com/likehua/p/2834669.html
Copyright © 2020-2023  润新知