- Apache Solr 教程
- Apache Solr - 主頁
- Apache Solr - 概覽
- Apache Solr - 搜尋引擎基礎
- Apache Solr - Windows 環境
- Apache Solr - 使用 Hadoop
- Apache Solr - 架構
- Apache Solr - 術語
- Apache Solr - 基本命令
- Apache Solr - 核心
- Apache Solr - 索引資料
- Apache Solr - 新增文件(XML)
- Apache Solr - 更新資料
- Apache Solr - 刪除文件
- Apache Solr - 檢索資料
- Apache Solr - 查詢資料
- Apache Solr - 分面搜尋
- Apache Solr 實用資源
- Apache Solr - 快速指南
- Apache Solr - 實用資源
- Apache Solr - 討論
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 = "https://: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}
廣告