標籤:solr solrj
一、SolrJ基礎
1、相關資料
API:http://lucene.apache.org/solr/4_9_0/solr-solrj/
apache_solr_ref_guide_4.9.pdf:Client APIs---Using SolrJ
http://wiki.apache.org/solr/Solrj
solr in action:Using the SolrJ client library to add documents from Java, Using SolrJ from Java
2、
二、SolrJ用於索引
三、SolrJ用於搜尋
package org.jediael.ui;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.jediael.util.Constants;public class ReturnResult {<span style="white-space:pre"></span>public static void main(String[] args) throws Exception {<span style="white-space:pre"></span>String serverUrl = (args != null && args.length > 0) ? args[0]<span style="white-space:pre"></span>: "http://" + Constants.IP + ":" + Constants.PORT<span style="white-space:pre"></span>+ "/solr/collection1";<span style="white-space:pre"></span>SolrServer solrServer = new HttpSolrServer(serverUrl);<span style="white-space:pre"></span>// 讀取輸入參數作為查詢關鍵字,若無關鍵字,則查詢全部內容。<span style="white-space:pre"></span>String queryString = (args != null && args.length > 1) ? args[1]<span style="white-space:pre"></span>: "*:*";<span style="white-space:pre"></span>SolrQuery solrQuery = new SolrQuery(queryString);<span style="white-space:pre"></span>// 定義使用哪個request<span style="white-space:pre"></span>// handler進行搜尋,若無指定,則使用預設的handler.預設是/select。若solrConfig.xml中無/select這個searchHandler,則返回以下錯誤<span style="white-space:pre"></span>solrQuery.set("qt", "/search");<span style="white-space:pre"></span>// solrQuery.setRows(5);<span style="white-space:pre"></span>QueryResponse resp = solrServer.query(solrQuery);<span style="white-space:pre"></span>SolrDocumentList hits = resp.getResults();<span style="white-space:pre"></span>for (SolrDocument doc : hits) {<span style="white-space:pre"></span>for (String fieldName : doc.getFieldNames()) {<span style="white-space:pre"></span>System.out.println(fieldName + " : " + doc.getFieldValue(fieldName) + " ");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>System.out.println("------------------------Next Document--------------------------------");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}1、使用SolrJ進行搜尋,基本步驟如下:
(1)建立一個SolrServer。
(2)建立一個SolrQuery,並使用set(String,String)進行參數的配置。
(3)調用SolrServer.query(solrQuery),返回QueryResponse。
(4)對QueryResponse進行分析處理。
2、以下語句用於指定使用哪個request handler進行搜尋,若無指定,則使用預設的handler.預設是/select。若solrConfig.xml中無/select這個searchHandler,則返回以下錯誤
<span style="font-family: Arial, Helvetica, sans-serif;">solrQuery.set("qt", "/search");</span>
HTTP Status 404 - /solr/collection1/select
type Status report
message /solr/collection1/select
description The requested resource is not available.
Apache Tomcat/7.0.54