Apache Solr - 分面

Apache Solr 中的分面(Faceting)是指将搜索结果分为不同的类别。在本章中,我们将讨论 Apache Solr 中可用的分面类型 −

  • 查询分面 − 它返回当前搜索结果中与给定查询匹配的文档数量。

  • 日期分面 −它返回属于特定日期范围内的文档数量。

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。

[Hadoop@localhost bin]$ ./post -c Solr_sample sample.csv

执行上述命令后,给定 .csv 文件中提到的所有文档都将上传到 Apache Solr。

现在让我们对集合/核心 my_core 上具有 0 行的字段 author 执行分面查询。

打开 Apache Solr 的 Web UI,在页面左侧选中复选框 facet,如以下屏幕截图所示。

Checkbox

选中复选框后,您将在以便传递 facet 搜索的参数。现在,作为查询的参数,传递以下值。

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

最后,通过单击 Execute Query 按钮执行查询。

Query Pass

执行时,将产生以下结果。

Author Result

它根据作者对索引中的文档进行分类,并指定每个作者贡献的书籍数量。

使用 Java 客户端 API 进行分面

以下是将文档添加到 Apache Solr 索引的 Java 程序。将此代码保存在名为 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 { 
    //准备 Solr 客户端
    String urlString = "http://localhost:8983/Solr/my_core";
    SolrClient Solr = new HttpSolrClient.Builder(urlString).build();
    
    //准备 Solr 文档
    SolrInputDocument doc = new SolrInputDocument();
    
    //String query = request.query;
    SolrQuery query = new SolrQuery();
    
    //设置查询字符串
    query.setQuery("*:*");
    
    //设置行数
    query.setRows(0);
    
    //添加 facet 字段
    query.addFacetField("author");
    
    //创建查询请求
    QueryRequest qryReq = new QueryRequest(query);
    
    //创建查询响应
    QueryResponse resp = qryReq.process(Solr);
    
    //检索响应字段
    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"); 
      } 
   } 
}

通过在终端中执行以下命令来编译上述代码 −

[Hadoop@localhost bin]$ javac HitHighlighting
[Hadoop@localhost 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)]]