zoukankan      html  css  js  c++  java
  • 【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 {
    
    	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);
    
    		// 读取输入參数作为查询keyword,若无keyword,则查询所有内容。
    		String queryString = (args != null && args.length > 1) ? args[1]
    				: "*:*";
    		SolrQuery solrQuery = new SolrQuery(queryString);
    		// 定义使用哪个request
    		// handler进行搜索,若无指定,则使用默认的handler.默认是/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进行搜索,基本过程例如以下:

    (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




  • 相关阅读:
    [转] websocket新版协议分析+python实现 & websocket 通信协议
    [转] html5演示
    新浪网内部讲师进阶研习会之笔记
    css3 animation属性
    【转】python多线程编程
    搭建selenium自动化环境步骤
    win10下搭建QTP测试环境
    cocos2dx跨平台环境
    Cocos2dx运行win32工程
    原型模式(prototype)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4295185.html
Copyright © 2011-2022 走看看