在Apache Solr中的構面或分組(faceting)指的是將搜尋結果分類到各種類別中。在本章中,我們將討論Apache Solr中可用的faceting型別 -
構面或分組(faceting)命令被新增到任何正常的Solr查詢請求,並且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。
[yiibai@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程式向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");
}
}
}
通過在終端中執行以下命令編譯上述程式碼 -
[yiibai@ubuntu:/usr/local/solr/bin]$ javac HitHighlighting.java
[yiibai@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)]]