Solr檢索數據
在本章中,我們將討論如何使用Java Client API檢索數據。假設有一個名為sample.csv的.csv文檔,其中包含以下內容。
001,9848022337,Hyderabad,Rajiv,Reddy
002,9848022338,Kolkata,Siddarth,Battacharya
003,9848022339,Delhi,Rajesh,Khanna
可以使用post
命令在核心-solr_sample
下對此數據編制索引。
[yiibai@ubuntu:/usr/local/solr]$ ./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(); } }
通過在終端中執行以下命令編譯上述代碼 -
[yiibai@ubuntu:/usr/local/solr]$ javac RetrievingData.java
[yiibai@ubuntu:/usr/local/solr]$ 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}
Solr查詢數據
除了存儲數據,Apache Solr還提供了一些在需要時查詢數據的功能。 Solr提供了一些參數,可以使用它們來在查詢存儲的數據。
在下表中,我們列出了Apache Solr中提供的各種常用的一些查詢參數。
參數 | 描述 |
---|---|
q | 這是Apache Solr的主要查詢參數,文檔根據它們與此參數中的術語的相似性來評分。 |
fq | 這個參數表示Apache Solr的過濾器查詢,將結果集限制為與此過濾器匹配的文檔。 |
start | start 參數表示頁面的起始偏移量,此參數的默認值為0 。 |
rows | 這個參數表示每頁要檢索的文檔的數量。此參數的默認值為10 。 |
sort | 這個參數指定由逗號分隔的字段列表,根據該列表對查詢的結果進行排序。 |
fl | 這個參數為結果集中的每個文檔指定返回的字段列表。 |
wt | 這個參數表示要查看響應結果的寫入程序的類型。 |
您可以查看所有這些參數作為查詢Apache Solr的選項。訪問Apache Solr的主頁。 在頁面的左側,單擊選項“查詢(Query)”。 在這里,可以查看查詢參數的字段。
檢索記錄
假設我們在 my_core
核心中有3
條記錄。要從所選核心中檢索特定記錄,則需要傳遞特定文檔的字段的名稱和值對。例如,如果要使用字段id
和值來檢索記錄,則需要將字段的名稱 - 值對作為參數q
的值傳遞為 - id:001
,然后執行查詢。
以同樣的方式,您可以通過將*:*
作為值傳遞給參數q
來檢索索引中的所有記錄,如下面的屏幕截圖所示。
從第二個記錄開始檢索
可以通過將1
作為值傳遞給參數start
來從第二條記錄中檢索記錄,如下面的屏幕截圖所示。
限制記錄數
可以通過在rows
參數中指定值來限制記錄數。例如,可以通過將值2
傳遞到參數行(row
),將查詢結果中的記錄總數限制為2
,如下面的屏幕截圖所示。
響應寫入器類型
可以通過從參數wt
的所提供的值中,選擇一個來獲取所需文檔類型的響應。
在上面的例子中,我們選擇了.csv
格式來獲取響應。
字段列表
如果想在結果文檔中顯示指定字段,則需要傳遞必填寫的字段列表,用逗號分隔,作為屬性fl
的值。
在以下示例中,嘗試檢索以下幾個字段: id
,phone
和first_name
。
Solr構面(faceting)
在Apache Solr中的構面或分組(faceting)指的是將搜索結果分類到各種類別中。在本章中,我們將討論Apache Solr中可用的faceting類型 -
- 查詢faceting - 返回當前搜索結果中與給定查詢匹配的文檔數。
- 日期faceting - 它返回在特定日期范圍內的文檔數。
構面或分組(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。
[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客戶端API進行構面
以下是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)]]