public List getUrlResults(List<String> strList,String serverUrl,pageResult result)throws MalformedURLException, SolrServerException{ HttpSolrServer server=new HttpSolrServer(serverUrl);//serverUrl 表示要连接的网址 List<String> urls=new ArrayList<String>(); if(strList.size()>0){ for(int i=0;i<strList.size();i++){ ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", strList.get(i));//*.* 表示查询条件 //params.set("defType", "edismax"); params.set("start", "0");//查询开始的索引 //params.set("df", "content");//设置查询的范围 params.set("fl", "url");//设置查询返回的结果 result.setPageSize(getFoundNums(strList.get(i).toString(), serverUrl)); params.set("rows",result.getPageSize()); //params.set("df","url"); QueryResponse solrResponse; try { solrResponse = server.query(params); SolrDocumentList results = solrResponse.getResults(); for (SolrDocument solrDocument:results) { String url = (String) solrDocument.getFieldValue("url"); urls.add(url); } } catch (SolrServerException e) { e.printStackTrace(); } } } return urls; } public List<NewsInfo> getResultsByRule(List<String> ruleList,String serverUrl)throws MalformedURLException, SolrServerException{ HttpSolrServer server=new HttpSolrServer(serverUrl); List<NewsInfo> urls=new ArrayList<NewsInfo>(); if(ruleList.size()>0){ for(int i=0;i<ruleList.size();i++){ ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", ruleList.get(i));//*.* //params.set("defType", "edismax"); params.set("start", "0"); params.set("df", "all"); params.set("fl", "title description ntime url sourceid id");//return fields //params.set("rows",2); //params.set("df","url"); QueryResponse solrResponse; try { solrResponse = server.query(params); SolrDocumentList results = solrResponse.getResults();
Integer counts = results.getNumFound(); //获取查找到的总条数 for (SolrDocument solrDocument:results) { NewsInfo news=new NewsInfo(); news.setDescription((String)solrDocument.getFieldValue("description")); news.setTitle(solrDocument.getFieldValue("title").toString()); news.setUrl((String)solrDocument.getFieldValue("url")); news.setnTime(solrDocument.getFieldValue("ntime").toString()); Integer j=Integer.valueOf(((ArrayList)solrDocument.getFieldValue("sourceid")).get(0).toString()); news.setSourceId(j); news.setId(Integer.valueOf(solrDocument.getFieldValue("id").toString())); urls.add(news); } } catch (SolrServerException e) { e.printStackTrace(); } } } return urls; }