• java程序中连接solr并设置参数等


    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; }
  • 相关阅读:
    Java中间件:淘宝网系统高性能利器(转)
    淘宝的数据库拆分(TDDL)(转)
    java web几种开发模式(转)
    C++模板【转】
    set[c++]
    C# jsonhelper
    Vector[C++]
    list[C++]
    map[C++]
    C[泊车管理系统]
  • 原文地址:https://www.cnblogs.com/hzwl-2015/p/4228989.html
Copyright © 2020-2023  润新知