【Solr專題之九】SolrJ教程

來源:互聯網
上載者:User

標籤: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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.