Solrj SolrQuery查询

SolrQuery有两种方式处理查询结果处理如下

第一种方式:普通方式处理查询结果

public class QueryDocsDemo {
//	public static final String SOLR_URL = "http://127.0.0.1:8983/solr";
  public static final String SOLR_URL = "http://127.0.0.1:8983/solr";

  public static void main(String[] args) throws SolrServerException, IOException {
    HttpSolrServer server = new HttpSolrServer(SOLR_URL);
    server.setMaxRetries(1);
    server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
    server.setConnectionTimeout(5000); // 5 seconds to establish TCP
    //正常情况下,以下参数无须设置
    //使用老版本solrj操作新版本的solr时,因为两个版本的javabin incompatible,所以需要设置Parser
    server.setParser(new XMLResponseParser());
    server.setSoTimeout(1000); // socket read timeout
    server.setDefaultMaxConnectionsPerHost(100);
    server.setMaxTotalConnections(100);
    server.setFollowRedirects(false); // defaults to false
    server.setAllowCompression(true);
    //使用ModifiableSolrParams传递参数
//		ModifiableSolrParams params = new ModifiableSolrParams();
//		// 127.0.0.1:8983/solr/select?q=video&fl=id,name,price&sort=price asc&start=0&rows=2&wt=json
//		// 设置参数,实现上面URL中的参数配置
//		// 查询关键词
//		params.set("q", "video");
//		// 返回信息
//		params.set("fl", "id,name,price,score");
//		// 排序
//		params.set("sort", "price asc");
//		// 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了
//		params.set("start", 2);
//		params.set("rows", 2);
//		// 返回格式
//		params.set("wt", "javabin");
//		QueryResponse response = server.query(params);

    //使用SolrQuery传递参数,SolrQuery的封装性更好
    server.setRequestWriter(new BinaryRequestWriter());
    SolrQuery query = new SolrQuery();
    query.setQuery("video");
    query.setFields("id","name","price","score");
    query.setSort("price", ORDER.asc);
    query.setStart(0);
    query.setRows(2);
//		query.setRequestHandler("/select");
    QueryResponse response = server.query( query );
    // 搜索得到的结果数
    System.out.println("Find:" + response.getResults().getNumFound());
    // 输出结果
    int iRow = 1;
    for (SolrDocument doc : response.getResults()) {
      System.out.println("----------" + iRow + "------------");
      System.out.println("id: " + doc.getFieldValue("id").toString());
      System.out.println("name: " + doc.getFieldValue("name").toString());
      System.out.println("price: "
          + doc.getFieldValue("price").toString());
      System.out.println("score: " + doc.getFieldValue("score"));
      iRow++;
    }
  }

第二种方式:采用POJOs方式处理查询结果

public class QueryBeanDemo {
  public static final String SOLR_URL = "http://127.0.0.1:8983/solr";

  public static void main(String[] args) throws SolrServerException,
      IOException {
    // http://127.0.0.1:8983/solr/collection1/select?q=description%3A%E6%80%BB%E7%9B%AE%E6%A0%87&facet=true&facet.field=author_s
    HttpSolrServer server = new HttpSolrServer(SOLR_URL);
    server.setMaxRetries(1);
    server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
    server.setConnectionTimeout(5000); // 5 seconds to establish TCP
    // server.setRequestWriter(new BinaryRequestWriter());

    SolrQuery query = new SolrQuery();
    query.setQuery("description:改革");
    query.setStart(0);
    query.setRows(2);
    query.setFacet(true);
    query.addFacetField("author_s");

    QueryResponse response = server.query(query);
    // 搜索得到的结果数
    System.out.println("Find:" + response.getResults().getNumFound());
    // 输出结果
    int iRow = 1;
    
    //response.getBeans存在BUG,将DocumentObjectBinder引用的Field应该为 org.apache.solr.client.solrj.beans.Field
    SolrDocumentList list = response.getResults();
    DocumentObjectBinderL binder = new DocumentObjectBinderL();
    List<NewsBean> beanList=binder.getBeans(NewsBean.class, list);
    for(NewsBean news:beanList){
      System.out.println(news.getId());
    }

    for (SolrDocument doc : response.getResults()) {
      System.out.println("----------" + iRow + "------------");
      System.out.println("id: " + doc.getFieldValue("id").toString());
      System.out.println("name: " + doc.getFieldValue("name").toString());
      iRow++;
    }
    for (FacetField ff : response.getFacetFields()) {
      System.out.println(ff.getName() + "," + ff.getValueCount() + ","
          + ff.getValues());
    }
  }
}

你可能感兴趣的:(Solr)