`
kavy
  • 浏览: 888413 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

solrj integration with Spring

 
阅读更多

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

分享到:
评论

相关推荐

    solr-solrj-6.1.0

    Solr-Solrj是Apache Lucene项目下的一个子项目,专门为Apache Solr搜索引擎提供Java客户端库。Solr是一款强大的全文检索服务器,而Solrj则是与之交互的Java API,使得开发人员能够轻松地在Java应用程序中集成Solr的...

    solr-solrj-4.10.3.jar和solr-solrj-5.0.0.jar

    SolrJ是Apache Solr项目的Java客户端库,它为与Solr服务器进行交互提供了便利的API。这个压缩包包含了两个版本的SolrJ库:solr-solrj-4.10.3.jar和solr-solrj-5.0.0.jar。这两个版本的差异主要在于对Solr服务器的...

    solrj工具类封装

    solrj工具类封装,包括条件批量查询,批量增删改,分段修改。

    solr-solrj 5.0.0 demo

    Solr-Solrj 5.0.0 是一个用于与Apache Solr进行交互的Java客户端库。在本文中,我们将深入探讨Solr-Solrj的使用、功能及其与自建Solr服务的集成,特别是涉及到中文分词的场景。 Apache Solr是一款流行的开源全文...

    SolrJ需要的jar包

    SolrJ是Apache Solr官方提供的Java客户端库,它使得Java开发者能够轻松地与Solr搜索引擎进行通信,执行查询、索引文档等操作。SolrJ的使用是大数据环境中实现高效全文检索和数据分析的关键组件。在Java项目中,正确...

    SolrJ6.3.0

    SolrJ 6.3.0 是一个针对 Apache Solr 的 Java 客户端库,它使得在 Java 应用程序中与 Solr 服务器进行交互变得更加便捷。Solr 是一个流行的开源搜索引擎,用于处理和索引大量文本数据,提供高效、可扩展的全文搜索...

    solrj的使用

    SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。Solr是一个高性能、可扩展的企业级全文搜索引擎,广泛用于内容索引和搜索。SolrJ提供了丰富的API,允许开发者...

    solr配置和solrJ的使用

    ### Solr配置与SolrJ使用详解 #### 一、Solr基本安装与配置 **1. 下载Solr** - **步骤说明**: 从Apache官方镜像站点下载Solr 1.4.1版本。 - **操作详情**: 访问链接`http://apache.etoak.com/lucene/solr/`,...

    solrJ 需要的jar文件 (全)

    SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr搜索引擎进行交互变得简单。这个压缩包文件包含了SolrJ运行所必需的一些关键库,包括JUnit测试框架、Commons IO和Commons Logging。接下来...

    solr-solrj-4.9.0.jar

    solr-solrj-4.9.0.jar

    solr-solrj-4.10.3.jar

    solr-solrj-4.10.3.jar。

    solrj使用教程

    SolrJ是Apache Solr官方提供的Java客户端库,它使得在Java应用程序中与Solr进行交互变得简单。这个“solrj使用教程”很可能是为了帮助开发者了解如何使用SolrJ来连接、查询和管理Solr索引。下面将详细介绍SolrJ的...

    solr-solrj-4.4.0.jar

    solr-solrj-4.4.0.jar

    solr-solrj-6.6.0.jar

    solr-solrj-6.6.0.jar

    java solr solrj 带账号密码增量查询添加索引

    主要讲解了 solr客户端如何调用带账号密码的solr服务器调用,实现添加索引和查询索引,以及分组查询

    solrj创建连接工厂

    solr部署在tomcat势必要设置用户权限,所以客户端solrj在创建连接的时候也需要用户名加密码

    solr详细配置教程与solrj的使用

    solr详细配置教程与solrj的使用

    solr-solrj-5.5.0.jar

    solrJ是Java连接solr进行查询检索和索引更新维护的jar包。

    solrJ所需要的核心包

    java大数据开发中solrJ所需要的核心包.................

    非常全面的solrj使用案例

    里面有非常详细的说明交你怎么使用solrj客户端来操作solrAPI

Global site tag (gtag.js) - Google Analytics