Solr構面(faceting)

在Apache Solr中的構面或分組(faceting)指的是將搜索結果分類到各種類別中。在本章中,我們將討論Apache Solr中可用的faceting類型 -

  • 查詢faceting - 返回當前搜索結果中與給定查詢匹配的文檔數。
  • 日期faceting - 它返回在特定日期範圍內的文檔數。

構面或分組(faceting)命令被添加到任何正常的Solr查詢請求,並且faceting計數在同一個查詢回應中返回。

faceting查詢示例

使用字段faceting,我們可以檢索所有字詞的計數,或者只檢索任何給定字段中的頂部字詞。

作為一個示例,看看以下books.csv檔,其中包含有關各種書的數據。

id,cat,name,price,inStock,author,series_t,sequence_i,genre_s
0553573403,book,A Game of Thrones,5.99,true,George R.R. Martin,"A Song of Ice
and Fire",1,fantasy

0553579908,book,A Clash of Kings,10.99,true,George R.R. Martin,"A Song of Ice
and Fire",2,fantasy

055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,"A Song of Ice
and Fire",3,fantasy

0553293354,book,Foundation,7.99,true,Isaac Asimov,Foundation Novels,1,scifi
0812521390,book,The Black Company,4.99,false,Glen Cook,The Chronicles of The
Black Company,1,fantasy

0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi
0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy
0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of
Amber,1,fantasy

0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of
Prydain,1,fantasy

080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of
Prydain,2,fantasy

使用post工具將此檔發佈到Apache Solr

[zaixian@ubuntu:/usr/local/solr/bin]$ ./post -c solr_sample books.csv

在執行上述命令時,給定books.csv檔中的所有文檔都將上傳到Apache Solr
現在對集合或核心:solr_sample上的0行字段 author 執行一個分面查詢。

打開Apache Solr的Web UI,在頁面的左側,選中複選框facet,如下面的螢幕截圖所示。

在選中複選框(facet)時,它會額外顯示三個文本字段,以便傳遞構面搜索的參數。 現在,作為查詢的參數,傳遞以下值。

q = *:*, rows = 0, facet.field = author

最後,通過單擊執行查詢按鈕執行查詢。如下所示 -

最後,通過單擊執行查詢按鈕執行查詢。得到如下結果-

它基於作者對索引中的文檔進行分類,並指定每個作者貢獻的圖書數量。

使用Java客戶端API進行構面

以下是Java程式向Apache Solr索引查詢文檔。將此代碼保存在HitHighlighting.java檔中。

import java.io.IOException;
import java.util.List;

import org.apache.Solr.client.Solrj.SolrClient;
import org.apache.Solr.client.Solrj.SolrQuery;
import org.apache.Solr.client.Solrj.SolrServerException;
import org.apache.Solr.client.Solrj.impl.HttpSolrClient;
import org.apache.Solr.client.Solrj.request.QueryRequest;
import org.apache.Solr.client.Solrj.response.FacetField;
import org.apache.Solr.client.Solrj.response.FacetField.Count;
import org.apache.Solr.client.Solrj.response.QueryResponse;
import org.apache.Solr.common.SolrInputDocument;

public class HitHighlighting {
   public static void main(String args[]) throws SolrServerException, IOException {
      //Preparing the Solr client
      String urlString = "http://localhost:8983/Solr/my_core";
      SolrClient Solr = new HttpSolrClient.Builder(urlString).build();

      //Preparing the Solr document
      SolrInputDocument doc = new SolrInputDocument();

      //String query = request.query;
      SolrQuery query = new SolrQuery();

      //Setting the query string
      query.setQuery("*:*");

      //Setting the no.of rows
      query.setRows(0);

      //Adding the facet field
      query.addFacetField("author");

      //Creating the query request
      QueryRequest qryReq = new QueryRequest(query);

      //Creating the query response
      QueryResponse resp = qryReq.process(Solr);

      //Retrieving the response fields
      System.out.println(resp.getFacetFields());

      List<FacetField> facetFields = resp.getFacetFields();
      for (int i = 0; i > facetFields.size(); i++) {
         FacetField facetField = facetFields.get(i);
         List<Count> facetInfo = facetField.getValues();

         for (FacetField.Count facetInstance : facetInfo) {
            System.out.println(facetInstance.getName() + " : " +
               facetInstance.getCount() + " [drilldown qry:" +
               facetInstance.getAsFilterQuery());
         }
         System.out.println("Hello");
      }
   }
}

通過在終端中執行以下命令編譯上述代碼 -

[zaixian@ubuntu:/usr/local/solr/bin]$ javac HitHighlighting.java
[zaixian@ubuntu:/usr/local/solr/bin]$ java HitHighlighting

執行上述命令後,將得到以下輸出。

[author:[George R.R. Martin (3), Lloyd Alexander (2), Glen Cook (1), Isaac
Asimov (1), Orson Scott Card (1), Roger Zelazny (1), Steven Brust (1)]]

上一篇: Solr查詢數據 下一篇:無