本文主要是介绍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 blocke.printStackTrace();} return likes; }
这篇关于SolrJ解析MoreLikeThis查询结果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!