solr Java 操作 solr query + facet

1:查询
       SolrJ提供的查询功能比较强大,可以进行结果中查询、范围查询、排序等。
       下面是笔者封装的一个查询函数:
      
       public static QueryResponse Search(String[] field, String[] key, int start,
                     int count, String[] sortfield, Boolean[] flag, Boolean hightlight) {
              //检测输入是否合法
              if (null == field || null == key || field.length != key.length) {
                     return null;
              }
              if (null == sortfield || null == flag || sortfield.length != flag.length) {
                     return null;
              }
             
              SolrQuery query = null;
              try {
                     //初始化查询对象
                     query = new SolrQuery(field[0] + ":" + key[0]);
                     for (int i = 0; i < field.length; i++) {
                            query.addFilterQuery(field[i] + ":" + key[i]);
                     }
                     //设置起始位置与返回结果数
                     query.setStart(start);
                     query.setRows(count);
                     //设置排序
                     for(int i=0; i");// 标记
                            query.setHighlightSimplePost("");
                            query.setHighlightSnippets(1);//结果分片数,默认为1
                            query.setHighlightFragsize(1000);//每个分片的最大长度,默认为100
                     }
              } catch (Exception e) {
                     e.printStackTrace();
              }
 
              QueryResponse rsp = null;
              try {
                     rsp = solr.query(query);
              } catch (Exception e) {
                     e.printStackTrace();
                     return null;
              }
              //返回查询结果
              return rsp;
       }
       补充一下范围查询的格式:[star t TO end],start与end是相应数据格式的值的字符串形式,“TO”     一定要保持大写!
 
2:  读取查询结果
       DocList读取返回结果:
       SolrDocumentList solrList = rsp.getResults();
 
       Beans读取返回结果:
       List tmpLists = rsp.getBeans(Item.class);
 
       读取高亮显示结果:
                     rsp = Search(field, key, 0, 10, sortfield, flag, true);
                     if(null == rsp){
                            return;
                     }
                     Map>> hightlight = rsp.getHighlighting();
                     //Item即为上面定义的bean类
                     List tmpLists = rsp.getBeans(Item.class);
                     for (int i = 0; i < tmpLists.size(); i++) {
                            //hightlight的键为Item的id,值唯一,我们设置的高亮字段为title
                            String hlString = hightlight.get(tmpLists.get(i).getId()).get("title").toString();
                            if (null != hlString) {
                                   System.out.println(hlString);
                            }
                     }
 
3:  Facet的一个应用:自动补全
       //prefix为前缀,min为最大返回结果数
       public static String[] autoComplete(String prefix, int min) {
              String words[] = null;
              StringBuffer sb = new StringBuffer("");
              SolrQuery query = new SolrQuery("*.*");
              QueryResponse rsp= new QueryResponse();
              //Facet为solr中的层次分类查询
              try {
                     query.setFacet(true);
                     query.setQuery("*:*");
                     query.setFacetPrefix(prefix);
                     query.addFacetField("title");
                     rsp = solr.query(query);
              } catch (Exception e) {
                     // TODO: handle exception
                     e.printStackTrace();
                     return null;
              }
             
              if(null != rsp){
                     FacetField ff = rsp.getFacetField("title");
                     List countList = ff.getValues();
                     if(null == countList){
                            return null;
                     }
                     for(int i=0; i

你可能感兴趣的:(solr)