• 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;		
    	}
    
  • 相关阅读:
    phpstrom+xdebug+chrome+postman调试工具搭建
    Docker常用命令
    使用nginx+docker配置https负载均衡
    LVS三种模式的区别及负载均衡算法
    ELK Betas 6.0安装及使用
    Excel从低级到中级
    我回来了,哈哈
    刷电信版s710d卡在htc开机画面的解决办法
    基于cocos2d-android-1的FlyppyBird
    利息?hii
  • 原文地址:https://www.cnblogs.com/likehua/p/2834669.html
Copyright © 2020-2023  润新知