【Solr专题之九】SolrJ教程
一、SolrJ基礎(chǔ)
1、相關(guān)資料
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 {public static void main(String[] args) throws Exception {String serverUrl = (args != null && args.length > 0) ? args[0]: "http://" + Constants.IP + ":" + Constants.PORT+ "/solr/collection1";SolrServer solrServer = new HttpSolrServer(serverUrl);// 讀取輸入?yún)?shù)作為查詢關(guān)鍵字,若無關(guān)鍵字,則查詢?nèi)績?nèi)容。String queryString = (args != null && args.length > 1) ? args[1]: "*:*";SolrQuery solrQuery = new SolrQuery(queryString);// 定義使用哪個request// handler進(jìn)行搜索,若無指定,則使用默認(rèn)的handler.默認(rèn)是/select。若solrConfig.xml中無/select這個searchHandler,則返回以下錯誤solrQuery.set("qt", "/search");// solrQuery.setRows(5);QueryResponse resp = solrServer.query(solrQuery);SolrDocumentList hits = resp.getResults();for (SolrDocument doc : hits) {for (String fieldName : doc.getFieldNames()) {System.out.println(fieldName + " : " + doc.getFieldValue(fieldName) + " ");}System.out.println("------------------------Next Document--------------------------------");}} }1、使用SolrJ進(jìn)行搜索,基本步驟如下:
(1)創(chuàng)建一個SolrServer。
(2)創(chuàng)建一個SolrQuery,并使用set(String,String)進(jìn)行參數(shù)的配置。
(3)調(diào)用SolrServer.query(solrQuery),返回QueryResponse。
(4)對QueryResponse進(jìn)行分析處理。
2、以下語句用于指定使用哪個request handler進(jìn)行搜索,若無指定,則使用默認(rèn)的handler.默認(rèn)是/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
總結(jié)
以上是生活随笔為你收集整理的【Solr专题之九】SolrJ教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】solr教程,值得刚接触搜索开发
- 下一篇: 【solr基础教程之九】客户端