Apache-solr-faceting

提供:Dev Guides
移動先:案内検索

Apache Solr-ファセット

Apache Solrのファセットとは、検索結果をさまざまなカテゴリに分類することです。 この章では、Apache Solrで利用可能なファセットの種類について説明します-

  • クエリファセット-指定されたクエリにも一致する現在の検索結果のドキュメントの数を返します。
  • 日付ファセット-特定の日付範囲内にあるドキュメントの数を返します。

ファセットコマンドは通常のSolrクエリリクエストに追加され、ファセットカウントは同じクエリ応答で返されます。

ファセットクエリの例

フィールド 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 をオンにします。

チェックボックス

チェックボックスをオンにすると、ファセット検索のパラメーターを渡すために、さらに3つのテキストフィールドがあります。 ここで、クエリのパラメーターとして、次の値を渡します。

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

最後に、[クエリの実行]ボタンをクリックしてクエリを実行します。

クエリパス

実行すると、次の結果が生成されます。

作成者の結果

著者に基づいてインデックス内のドキュメントを分類し、各著者が投稿した書籍の数を指定します。

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 {
     //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");
      }
   }
}

端末で次のコマンドを実行して、上記のコードをコンパイルします-

[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)]]