1. 程式人生 > >solr Java 操作 solr query + facet

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<sortfield.length; i++){
                            if (flag[i]) {
                                   query.addSortField(sortfield[i], SolrQuery.ORDER.asc);
                            } else {
                                   query.addSortField(sortfield[i], SolrQuery.ORDER.desc);
                            }
                     }
                     //設定高亮
                     if (null != hightlight) {
                            query.setHighlight(true); // 開啟高亮元件
                            query.addHighlightField("title");// 高亮欄位
                            query.setHighlightSimplePre("<font color="red">");// 標記
                            query.setHighlightSimplePost("</font>");
                            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<Item> tmpLists = rsp.getBeans(Item.class);
 
       讀取高亮顯示結果:
                     rsp = Search(field, key, 0, 10, sortfield, flag, true);
                     if(null == rsp){
                            return;
                     }
                     Map<String, Map<String, List<String>>> hightlight = rsp.getHighlighting();
                     //Item即為上面定義的bean類
                     List<Item> 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<Count> countList = ff.getValues();
                     if(null == countList){
                            return null;
                     }
                     for(int i=0; i<countList.size(); i++){
                            String tmp[] = countList.get(i).toString().split(" ");
                            //排除單個字
                            if(tmp[0].length()< 2){
                                   continue;
                            }
                            sb.append(tmp[0] + " ");
                            min--;
                            if(min == 0){
                                   break;
                            }
                     }
                     words = sb.toString().split(" ");
              }else{
                     return null;
              }
              return words;
       }