Apache-solr-retrieving-data

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

Apache Solr-データの取得

この章では、JavaクライアントAPIを使用してデータを取得する方法について説明します。 次の内容を持つ sample.csv という名前の.csvドキュメントがあるとします。

001,9848022337,Hyderabad,Rajiv,Reddy
002,9848022338,Kolkata,Siddarth,Battacharya
003,9848022339,Delhi,Rajesh,Khanna
*post* コマンドを使用して、 *sample_Solr* という名前のコアの下でこのデータにインデックスを付けることができます。
[Hadoop@localhost bin]$ ./post -c Solr_sample sample.csv

以下は、Apache Solrインデックスにドキュメントを追加するJavaプログラムです。 このコードを RetrievingData.java という名前のファイルに保存します。

import java.io.IOException;

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.response.QueryResponse;
import org.apache.Solr.common.SolrDocumentList;

public class RetrievingData {
   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 Solr query
      SolrQuery query = new SolrQuery();
      query.setQuery("*:*");

     //Adding the field to be retrieved
      query.addField("*");

     //Executing the query
      QueryResponse queryResponse = Solr.query(query);

     //Storing the results of the query
      SolrDocumentList docs = queryResponse.getResults();
      System.out.println(docs);
      System.out.println(docs.get(0));
      System.out.println(docs.get(1));
      System.out.println(docs.get(2));

     //Saving the operations
      Solr.commit();
   }
}

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

[Hadoop@localhost bin]$ javac RetrievingData
[Hadoop@localhost bin]$ java RetrievingData

上記のコマンドを実行すると、次の出力が得られます。

{numFound = 3,start = 0,docs = [SolrDocument{id=001, phone = [9848022337],
city = [Hyderabad], first_name = [Rajiv], last_name = [Reddy],
_version_ = 1547262806014820352}, SolrDocument{id = 002, phone = [9848022338],
city = [Kolkata], first_name = [Siddarth], last_name = [Battacharya],

_version_ = 1547262806026354688}, SolrDocument{id = 003, phone = [9848022339],
city = [Delhi], first_name = [Rajesh], last_name = [Khanna],

_version_ = 1547262806029500416}]}

SolrDocument{id = 001, phone = [9848022337], city = [Hyderabad], first_name = [Rajiv],
last_name = [Reddy], _version_ = 1547262806014820352}

SolrDocument{id = 002, phone = [9848022338], city = [Kolkata], first_name = [Siddarth],
last_name = [Battacharya], _version_ = 1547262806026354688}

SolrDocument{id = 003, phone = [9848022339], city = [Delhi], first_name = [Rajesh],
last_name = [Khanna], _version_ = 1547262806029500416}