solrj for solr5.3.1(solrj最新版本API)

0.前言

solrj是solr的客户端java版本,总所周知solr支持http的api接口方式来操作包括索引、搜索等功能;
同样支持编码的方式来完成相关操作。 --caicongyang

1.添加相关jar包

可以添加solr下所有jar包

亦可以只添加以下:(差缺补漏)

solr-core-5.3.1.jar
solr-solrj-5.3.1.jar
httpclient-4.4.1.jar

httpcore-4.4.1.jar

httpmime-4.4.1.jar

slf4j-api-1.7.7.jar
slf4j-log4j12-1.7.7.jar

2.相关代码

package com.ccy.solr;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.MultiMapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.servlet.SolrRequestParsers;
import org.junit.Test;

/**
 * 
 * 

* Title: SolrJTest.java * Package com.ccy.solr *

*

* Description: solr5.3.1中的solrj相关api demo测试类 * *

* @author Tom.Cai * @created 2015-11-30 下午10:29:46 * @version V1.0 * */ public class SolrJTest { /** * 创建solrClient (4.x的版本使用类是SolrServer,在新版本中已经被弃用了) */ @Test public void createSolrClient(){ try { SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/db"); SolrPingResponse response = solr.ping(); //打印执行时间 System.out.println(response.getElapsedTime()); solr.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 增加bean索引,该bean中的字段需要匹配schema中的fields,(可以用@Field注解来关联相关字段) * 否者抛出org.apache.solr.client.solrj.beans.BindingException: class: class com.ccy.solr.Blog does not define any fields. * @throws Exception */ @Test public void addBeanIndex() throws Exception{ SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/db"); Blog blog = new Blog(); blog.setId(123); blog.setTitle("test"); blog.setContent("test..."); blog.setKeyWord("test"); UpdateResponse response = solr.addBean(blog); System.out.println(response.getElapsedTime()); solr.commit(); solr.close(); } /** * 增加索引 * * @throws Exception */ @Test public void addIndex() throws Exception{ SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/solr"); SolrInputDocument document = new SolrInputDocument(); document.addField("id",123, new Float(1.0)); document.addField("content", "test"); UpdateResponse response = solr.add(document); System.out.println(response.getElapsedTime()); solr.commit(); solr.close(); } /** * 删除索引 */ @Test public void delIndex() throws Exception{ SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/solr"); UpdateResponse response = solr.deleteById("123"); System.out.println(response.getElapsedTime()); solr.commit(); solr.close(); } /** * 简单查询 * @throws IOException * @throws SolrServerException */ @Test public void query() throws Exception{ SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/solr"); Map map = new HashMap(); map.put("q", "content:test"); SolrParams params = new MapSolrParams(map); QueryResponse resp = solr.query(params); //以下是第二种方法 //String queryString="content:test"; //MultiMapSolrParams mParams = SolrRequestParsers.parseQueryString("queryString"); //QueryResponse resp = solr.query(mParams); SolrDocumentList docsList = resp.getResults(); System.out.println(docsList.size()); for (SolrDocument doc : docsList) { System.out.println(doc.get("id")); } solr.close(); } }


3.随记

Eclipse 自动生成还回值对象快捷键 :Alt+Shift+l

4.其他

记录与分享,你我共成长!关于solr的其他文章可以查看我的博客:caicongyang 

http://blog.csdn.net/caicongyang




你可能感兴趣的:(搜索/爬虫)