一、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>
type Status report
message /solr/collection1/select
description The requested resource is not available.
【Solr专题之九】SolrJ教程,布布扣,bubuko.com
原文地址:http://blog.csdn.net/jediael_lu/article/details/38229323