直接上代码:
/** * 解析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; }