Solr记录-solr检索和查询数据

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
R

以下是向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();         
   } 
}
Java

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

[yiibai@ubuntu:/usr/local/solr]$ javac RetrievingData.java
[yiibai@ubuntu:/usr/local/solr]$ java RetrievingData
Shell

执行上述命令后,将得到以下输出。

{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的值。

在以下示例中,尝试检索以下几个字段: idphonefirst_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
Bash

使用post工具将此文件发布到Apache Solr

[yiibai@ubuntu:/usr/local/solr/bin]$ ./post -c solr_sample books.csv
C

在执行上述命令时,给定books.csv文件中的所有文档都将上传到Apache Solr
现在对集合或核心:solr_sample上的0行字段 author 执行一个分面查询。

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

在选中复选框(facet)时,它会额外显示三个文本字段,以便传递构面搜索的参数。 现在,作为查询的参数,传递以下值。

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

最后,通过单击执行查询按钮执行查询。如下所示 -

最后,通过单击执行查询按钮执行查询。得到如下结果-

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

使用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"); 
      } 
   } 
}
Java

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

[yiibai@ubuntu:/usr/local/solr/bin]$ javac HitHighlighting.java
[yiibai@ubuntu:/usr/local/solr/bin]$ java HitHighlighting
Shell

执行上述命令后,将得到以下输出。

[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)]]
 
posted @ 2017-10-24 10:43  信方  阅读(25626)  评论(0编辑  收藏  举报