The first one is EmbeddedSolrServer, so I do not need to start my standalone solr server, I can run the junit tests.
The pom.xml are as follow:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
</dependency>
package com.sillycat.easyhunter.plugin.solr;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.CoreContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
public class EmbeddedSolrServerTest {
EmbeddedSolrServer server = null;
@Before
public void init() {
System.setProperty("solr.solr.home",
"D:\\book\\solr\\apache-solr-3.6.0\\example\\solr");
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = null;
try {
coreContainer = initializer.initialize();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
server = new EmbeddedSolrServer(coreContainer, "");
}
@After
public void destory() {
server = null;
System.runFinalization();
System.gc();
}
public final void fail(Object o) {
System.out.println(o);
}
@Test
public void server() {
fail(server);
}
@Test
public void query() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = server.query(params);
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
CommonsHttpSolrServer is under deprecation. So in 3.6.0, I change to use HttpSolrServer.
package com.sillycat.easyhunter.plugin.solr;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HttpSolrServerTest {
private HttpSolrServer server;
private static final String DEFAULT_URL = "http://localhost:8983/solr/";
@Before
public void init() {
server = new HttpSolrServer(DEFAULT_URL);
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
//sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(new XMLResponseParser());
}
@After
public void destory() {
server = null;
System.runFinalization();
System.gc();
}
public final void fail(Object o) {
System.out.println(o);
}
@Test
public void server() {
fail(server);
}
@Test
public void query() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = server.query(params);
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
It is not very simple to use these things like this, so I try to write a factory class for solrserver.
package com.sillycat.easyhunter.plugin.solr;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.core.CoreContainer;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.xml.sax.SAXException;
import com.sillycat.easyhunter.common.StringUtil;
public class SolrServerFactory implements FactoryBean<SolrServer>,
InitializingBean {
protected final Log log = LogFactory.getLog(getClass());
private final String DEFAULT_SERVER_URL = "http://localhost:8983/solr/";
private final String DEFAULT_SOLR_HOME = "D:\\book\\solr\\apache-solr-3.6.0\\example\\solr";
private final String DEFAULT_SOLR_SERVER_CLASS_NAME = "org.apache.solr.client.solrj.embedded.EmbeddedSolrServer";
private String serverURL;
private String solrHome;
private String solrServerClassName;
private SolrServer solrServer = null;
public SolrServer getObject() throws Exception {
return solrServer;
}
public Class<SolrServer> getObjectType() {
return SolrServer.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
if ("org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())) {
System.setProperty("solr.solr.home", this.getSolrHome());
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = null;
try {
coreContainer = initializer.initialize();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");
solrServer = server;
} else if ("org.apache.solr.client.solrj.impl.HttpSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())) {
HttpSolrServer server = new HttpSolrServer(this.getServerURL());
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any
// effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
// sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(new XMLResponseParser());
solrServer = server;
}
}
public String getServerURL() {
if (StringUtil.isBlank(serverURL)) {
serverURL = DEFAULT_SERVER_URL;
}
return serverURL;
}
public void setServerURL(String serverURL) {
this.serverURL = serverURL;
}
public String getSolrHome() {
if (StringUtil.isBlank(solrHome)) {
solrHome = DEFAULT_SOLR_HOME;
}
return solrHome;
}
public void setSolrHome(String solrHome) {
this.solrHome = solrHome;
}
public String getSolrServerClassName() {
if (StringUtil.isBlank(solrServerClassName)) {
solrServerClassName = DEFAULT_SOLR_SERVER_CLASS_NAME;
}
return solrServerClassName;
}
public void setSolrServerClassName(String solrServerClassName) {
this.solrServerClassName = solrServerClassName;
}
}
The spring configuration will be
<bean id="httpSolrServer" class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<property name="solrServerClassName" value="org.apache.solr.client.solrj.impl.HttpSolrServer"/>
<property name="serverURL" value="http://localhost:8983/solr/" />
</bean>
<bean id="embeddedSolrServer" class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<property name="solrServerClassName" value="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"/>
<property name="solrHome" value="D:\\book\\solr\\apache-solr-3.6.0\\example\\solr" />
</bean>
The test case can be as follow:
package com.sillycat.easyhunter.plugin.solr;
import junit.framework.Assert;
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.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/test-context.xml" })
public class SolrServerFactoryTest {
@Autowired
@Qualifier("httpSolrServer")
private SolrServer httpSolrServer;
@Autowired
@Qualifier("embeddedSolrServer")
private SolrServer embeddedSolrServer;
@Test
public void dumy() {
Assert.assertTrue(true);
}
@Test
public void embeddedSolrServerQuery() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = embeddedSolrServer.query(params);
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
Assert.assertNotNull(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
//@Test
public void httpSolrServerQuery() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = httpSolrServer.query(params);
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
Assert.assertNotNull(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
references:
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://wiki.apache.org/solr/Solrj
http://groovy.codehaus.org/Grape
http://www.java2s.com/Code/Java/Spring/MessageDigestExample.htm
http://stackoverflow.com/questions/234600/can-i-use-class-newinstance-with-constructor-arguments
- 浏览: 888525 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
zzuliuli:
很实用,一直关注
mysql的执行计划 -
rxin2009:
你好,最近在解决redis数据同步的问题,找到了tedis,但 ...
taobao/tedis的redis集群 -
zhangping2056:
楼主接下来要考虑页面静态化与细节上面的东西了
Nginx与Redis解决高并发问题 -
XieFuQ:
Tomcat的重启shell脚本 -
jovinlee:
jovinlee 写道 jov ...
Tomcat的重启shell脚本
发表评论
-
Spring Boot 架构
2020-06-03 09:54 0架构实战篇(一):Spring Boot 整合MyB ... -
Redis单线程的正确理解
2020-06-02 09:27 359很多同学对Redis的单线 ... -
SpringBoot2.x整合线程池(ThreadPoolTaskExecutor)
2020-05-26 18:39 1073JAVA && Spring & ... -
浅析springboot自动配置原理
2020-05-26 15:48 355梦里寻他千百度—— ... -
使用redis管道(pipeline)实现批量查询,批量修改
2020-04-23 08:48 2858Pipeline:“管道”,和很多设计模式中的“管道”具有 ... -
Idea快捷键大全
2019-12-24 16:12 391Ctrl 快捷键介绍 Ctrl + ... -
图解Tomcat类加载机制
2019-10-18 09:39 0https://www.cnblogs.com/aspira ... -
kafka查询和修改topic的offset
2019-09-25 08:59 0查询topic的offset的范围 用下面命令可以查询到t ... -
kafka基本原理介绍,以及重新选举,replica复制机制,isr等
2019-09-24 17:13 0https://blog.csdn.net/dshf_1/ ... -
Zookeeper 在 Kafka 中的作用
2019-09-24 16:13 0leader 选举 和 follower 信息同步 ... -
netty4多连接客户端设计与实现
2019-09-22 21:16 0版权声明:本文为博主原创文章,遵循 CC 4.0 BY-S ... -
数据一致性
2019-09-10 08:35 0缓存与数据库的数据 ... -
JVM GC原理
2019-09-06 09:22 0https://www.cnblogs.com/yy3b ... -
分布式存储系统概要
2019-09-05 09:07 0http://witchiman.top/2017/05/0 ... -
Kafka 0.11.0.0 是如何实现 Exactly-once 语义的
2019-09-04 09:42 0https://www.jianshu.com/p/5d88 ... -
分布式系统的经典基础理论
2019-09-02 14:11 0历史优质文章: 可能是最漂亮的Spring事务管理详解 ... -
分布式系统的经典基础理论——中心化与去中心化
2019-09-02 10:18 0分布式系统从诞生发展到现在已经走过十几个年头了,其中伴随着一 ... -
服务注册中心,Eureka与Zookeeper比较
2019-09-02 10:17 01. 前言 服务注册中心,给客户端提供可供调用的服 ... -
Redis分布式锁
2019-08-30 17:33 0http://www.redis.cn/topics/di ... -
Springboot-Redis分布式锁
2019-08-30 17:30 0随着现在分布式架构越 ...
相关推荐
Solr-Solrj是Apache Lucene项目下的一个子项目,专门为Apache Solr搜索引擎提供Java客户端库。Solr是一款强大的全文检索服务器,而Solrj则是与之交互的Java API,使得开发人员能够轻松地在Java应用程序中集成Solr的...
SolrJ是Apache Solr项目的Java客户端库,它为与Solr服务器进行交互提供了便利的API。这个压缩包包含了两个版本的SolrJ库:solr-solrj-4.10.3.jar和solr-solrj-5.0.0.jar。这两个版本的差异主要在于对Solr服务器的...
solrj工具类封装,包括条件批量查询,批量增删改,分段修改。
Solr-Solrj 5.0.0 是一个用于与Apache Solr进行交互的Java客户端库。在本文中,我们将深入探讨Solr-Solrj的使用、功能及其与自建Solr服务的集成,特别是涉及到中文分词的场景。 Apache Solr是一款流行的开源全文...
SolrJ是Apache Solr官方提供的Java客户端库,它使得Java开发者能够轻松地与Solr搜索引擎进行通信,执行查询、索引文档等操作。SolrJ的使用是大数据环境中实现高效全文检索和数据分析的关键组件。在Java项目中,正确...
SolrJ 6.3.0 是一个针对 Apache Solr 的 Java 客户端库,它使得在 Java 应用程序中与 Solr 服务器进行交互变得更加便捷。Solr 是一个流行的开源搜索引擎,用于处理和索引大量文本数据,提供高效、可扩展的全文搜索...
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。Solr是一个高性能、可扩展的企业级全文搜索引擎,广泛用于内容索引和搜索。SolrJ提供了丰富的API,允许开发者...
### Solr配置与SolrJ使用详解 #### 一、Solr基本安装与配置 **1. 下载Solr** - **步骤说明**: 从Apache官方镜像站点下载Solr 1.4.1版本。 - **操作详情**: 访问链接`http://apache.etoak.com/lucene/solr/`,...
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。这个压缩包文件包含了SolrJ运行所必需的一些关键库,包括JUnit测试框架、Commons IO和Commons Logging。接下来...
solr-solrj-4.9.0.jar
solr-solrj-4.10.3.jar。
SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr进行交互变得简单。这个“solrj使用教程”很可能是为了帮助开发者了解如何使用SolrJ来连接、查询和管理Solr索引。下面将详细介绍SolrJ的...
solr-solrj-4.4.0.jar
solr-solrj-6.6.0.jar
主要讲解了 solr客户端如何调用带账号密码的solr服务器调用,实现添加索引和查询索引,以及分组查询
solr部署在tomcat势必要设置用户权限,所以客户端solrj在创建连接的时候也需要用户名加密码
solr详细配置教程与solrj的使用
solrJ是Java连接solr进行查询检索和索引更新维护的jar包。
java大数据开发中solrJ所需要的核心包.................
里面有非常详细的说明交你怎么使用solrj客户端来操作solrAPI