java调用solr4.10 代码

需要的jar包

httpclient-4.3.1.jar

httpcore-4.3.jar

httpmime-4.3.1.jar

solr-core-4.10.2.jar

solr-solrj-4.10.2.jar

noggit-0.5.jar

 

 

一个简单的java调用

 

package util;

import java.net.MalformedURLException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
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.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.servlet.SolrRequestParsers;

import tea.entity.wotrip.Hacatering;

/** 
 * @author zihai367
 * @version 创建时间:2015-4-21 下午05:19:34 
 * 类说明 
 */
public class SolrExample {

	SolrServer server = new HttpSolrServer("http://127.0.0.1:8080/solr/solrArticle");  
	
	public void query01(String queryString) {
        SolrParams solrParams = SolrRequestParsers
                .parseQueryString(queryString);
        try {
            QueryResponse rsp = server.query(solrParams);
            List<Hacatering> results = rsp.getBeans(Hacatering.class);
            for (Hacatering bean : results)
                System.out.println(bean.toString());
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }
	
	public void query02(String queryString) {
        ModifiableSolrParams solrParams = new ModifiableSolrParams();
        solrParams.add("q", queryString);
        solrParams.add("start", "0");
        solrParams.add("rows", "10");
        try {
            QueryResponse rsp = server.query(solrParams);
            List<Hacatering> results = rsp.getBeans(Hacatering.class);
            for (Hacatering bean : results)
                System.out.println(bean.toString());
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
	}
	

	public void query03(String queryString) {
		SolrQuery query = new SolrQuery();
        query.setQuery(queryString);
        query.setStart(0);
        query.setRows(10);
        QueryResponse rsp;
        try {
        	rsp = server.query(query);
            List<Hacatering> results = rsp.getBeans(Hacatering.class);
            for (Hacatering bean : results)
                System.out.println(bean.toString());
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
	    }
	
	/*public static void main(String[] args) throws MalformedURLException, SolrServerException {
		SolrExample solrExample = new SolrExample();
		solrExample.query03("格林豪泰4");
	}*/
	
	public static void main(String[] args) throws MalformedURLException, SolrServerException {  
		  
	    SolrServer server = new HttpSolrServer("http://127.0.0.1:8080/solr/solrArticle");  
	  
	    ModifiableSolrParams params = new ModifiableSolrParams();  
	    // 查询关键词  
	    params.set("q", "格林豪泰4");  
	    // 分页,,start=0就是从0开始,,rows=5当前返回5条记录,,,第二页就是变化start这个值为5就可以了。  
	    params.set("start", 0);  
	    params.set("rows", 5);  
	  
	    // 排序,,如果按照id 排序,,那么将score desc 改成 id desc(or asc)  
	    //params.set("sort", "cid desc");  
	  
	    // 返回信息  * 为全部  这里是全部加上score,如果不加下面就不能使用score  
	    //params.set("fl", "*,cid");  
	      
	    QueryResponse response = server.query(params);  
	    // 搜索得到的结果数  
	    System.out.println("Find:"+ response.getResults().getNumFound()+"\n\n");  
	  
	    // 输出结果  
	    for(SolrDocument doc:response.getResults())  
	    {  
	        System.out.println("id: " + doc.getFieldValue("cid").toString());  
	        System.out.println("title: " + doc.getFieldValue("cname").toString()+"\n");  
	    }  
	  }
	
}

 

你可能感兴趣的:(代码,java调用solr4.10)