solr(2)solrj integration with Spring
solr(2)solrjintegrationwithSpring
Iwanttousesolrjtoworkwithmystandalonesolrserver.Accordingtothelatestdocument,Iwrite2testcase:
ThefirstoneisEmbeddedSolrServer,soIdonotneedtostartmystandalonesolrserver,Icanrunthejunittests.
Thepom.xmlareasfollow:
<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>
packagecom.sillycat.easyhunter.plugin.solr;
importjava.io.IOException;
importjavax.xml.parsers.ParserConfigurationException;
importorg.apache.solr.client.solrj.SolrQuery;
importorg.apache.solr.client.solrj.SolrServerException;
importorg.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
importorg.apache.solr.client.solrj.response.QueryResponse;
importorg.apache.solr.common.SolrDocumentList;
importorg.apache.solr.common.params.SolrParams;
importorg.apache.solr.core.CoreContainer;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.xml.sax.SAXException;
publicclassEmbeddedSolrServerTest{
EmbeddedSolrServerserver=null;
@Before
publicvoidinit(){
System.setProperty("solr.solr.home",
"D:\\book\\solr\\apache-solr-3.6.0\\example\\solr");
CoreContainer.Initializerinitializer=newCoreContainer.Initializer();
CoreContainercoreContainer=null;
try{
coreContainer=initializer.initialize();
}catch(IOExceptione){
e.printStackTrace();
}catch(ParserConfigurationExceptione){
e.printStackTrace();
}catch(SAXExceptione){
e.printStackTrace();
}
server=newEmbeddedSolrServer(coreContainer,"");
}
@After
publicvoiddestory(){
server=null;
System.runFinalization();
System.gc();
}
publicfinalvoidfail(Objecto){
System.out.println(o);
}
@Test
publicvoidserver(){
fail(server);
}
@Test
publicvoidquery(){
Stringquery="name:DDR";
SolrParamsparams=newSolrQuery(query);
try{
QueryResponseresponse=server.query(params);
SolrDocumentListlist=response.getResults();
for(inti=0;i<list.size();i++){
fail(list.get(i));
}
}catch(SolrServerExceptione){
e.printStackTrace();
}
}
}
CommonsHttpSolrServerisunderdeprecation.Soin3.6.0,IchangetouseHttpSolrServer.
packagecom.sillycat.easyhunter.plugin.solr;
importorg.apache.solr.client.solrj.SolrQuery;
importorg.apache.solr.client.solrj.SolrServerException;
importorg.apache.solr.client.solrj.impl.HttpSolrServer;
importorg.apache.solr.client.solrj.impl.XMLResponseParser;
importorg.apache.solr.client.solrj.response.QueryResponse;
importorg.apache.solr.common.SolrDocumentList;
importorg.apache.solr.common.params.SolrParams;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
publicclassHttpSolrServerTest{
privateHttpSolrServerserver;
privatestaticfinalStringDEFAULT_URL="http://localhost:8983/solr/";
@Before
publicvoidinit(){
server=newHttpSolrServer(DEFAULT_URL);
server.setSoTimeout(1000);//socketreadtimeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false);//defaultstofalse
//allowCompressiondefaultstofalse.
//Serversidemustsupportgzipordeflateforthistohaveanyeffect.
server.setAllowCompression(true);
server.setMaxRetries(1);//defaultsto0.>1notrecommended.
//sorlrJ目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(newXMLResponseParser());
}
@After
publicvoiddestory(){
server=null;
System.runFinalization();
System.gc();
}
publicfinalvoidfail(Objecto){
System.out.println(o);
}
@Test
publicvoidserver(){
fail(server);
}
@Test
publicvoidquery(){
Stringquery="name:DDR";
SolrParamsparams=newSolrQuery(query);
try{
QueryResponseresponse=server.query(params);
SolrDocumentListlist=response.getResults();
for(inti=0;i<list.size();i++){
fail(list.get(i));
}
}catch(SolrServerExceptione){
e.printStackTrace();
}
}
}
Itisnotverysimpletousethesethingslikethis,soItrytowriteafactoryclassforsolrserver.
packagecom.sillycat.easyhunter.plugin.solr;
importjava.io.IOException;
importjavax.xml.parsers.ParserConfigurationException;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.apache.solr.client.solrj.SolrServer;
importorg.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
importorg.apache.solr.client.solrj.impl.HttpSolrServer;
importorg.apache.solr.client.solrj.impl.XMLResponseParser;
importorg.apache.solr.core.CoreContainer;
importorg.springframework.beans.factory.FactoryBean;
importorg.springframework.beans.factory.InitializingBean;
importorg.xml.sax.SAXException;
importcom.sillycat.easyhunter.common.StringUtil;
publicclassSolrServerFactoryimplementsFactoryBean<SolrServer>,
InitializingBean{
protectedfinalLoglog=LogFactory.getLog(getClass());
privatefinalStringDEFAULT_SERVER_URL="http://localhost:8983/solr/";
privatefinalStringDEFAULT_SOLR_HOME="D:\\book\\solr\\apache-solr-3.6.0\\example\\solr";
privatefinalStringDEFAULT_SOLR_SERVER_CLASS_name="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer";
privateStringserverURL;
privateStringsolrHome;
privateStringsolrServerClassName;
privateSolrServersolrServer=null;
publicSolrServergetObject()throwsException{
returnsolrServer;
}
publicClass<SolrServer>getObjectType(){
returnSolrServer.class;
}
publicbooleanisSingleton(){
returntrue;
}
publicvoidafterPropertiesSet()throwsException{
if("org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())){
System.setProperty("solr.solr.home",this.getSolrHome());
CoreContainer.Initializerinitializer=newCoreContainer.Initializer();
CoreContainercoreContainer=null;
try{
coreContainer=initializer.initialize();
}catch(IOExceptione){
e.printStackTrace();
}catch(ParserConfigurationExceptione){
e.printStackTrace();
}catch(SAXExceptione){
e.printStackTrace();
}
EmbeddedSolrServerserver=newEmbeddedSolrServer(coreContainer,"");
solrServer=server;
}elseif("org.apache.solr.client.solrj.impl.HttpSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())){
HttpSolrServerserver=newHttpSolrServer(this.getServerURL());
server.setSoTimeout(1000);//socketreadtimeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false);//defaultstofalse
//allowCompressiondefaultstofalse.
//Serversidemustsupportgzipordeflateforthistohaveany
//effect.
server.setAllowCompression(true);
server.setMaxRetries(1);//defaultsto0.>1notrecommended.
//sorlrJ目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(newXMLResponseParser());
solrServer=server;
}
}
publicStringgetServerURL(){
if(StringUtil.isBlank(serverURL)){
serverURL=DEFAULT_SERVER_URL;
}
returnserverURL;
}
publicvoidsetServerURL(StringserverURL){
this.serverURL=serverURL;
}
publicStringgetSolrHome(){
if(StringUtil.isBlank(solrHome)){
solrHome=DEFAULT_SOLR_HOME;
}
returnsolrHome;
}
publicvoidsetSolrHome(StringsolrHome){
this.solrHome=solrHome;
}
publicStringgetSolrServerClassName(){
if(StringUtil.isBlank(solrServerClassName)){
solrServerClassName=DEFAULT_SOLR_SERVER_CLASS_NAME;
}
returnsolrServerClassName;
}
publicvoidsetSolrServerClassName(StringsolrServerClassName){
this.solrServerClassName=solrServerClassName;
}
}
Thespringconfigurationwillbe
<beanid="httpSolrServer"class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<propertyname="solrServerClassName"value="org.apache.solr.client.solrj.impl.HttpSolrServer"/>
<propertyname="serverURL"value="http://localhost:8983/solr/"/>
</bean>
<beanid="embeddedSolrServer"class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<propertyname="solrServerClassName"value="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"/>
<propertyname="solrHome"value="D:\\book\\solr\\apache-solr-3.6.0\\example\\solr"/>
</bean>
Thetestcasecanbeasfollow:
packagecom.sillycat.easyhunter.plugin.solr;
importjunit.framework.Assert;
importorg.apache.solr.client.solrj.SolrQuery;
importorg.apache.solr.client.solrj.SolrServer;
importorg.apache.solr.client.solrj.SolrServerException;
importorg.apache.solr.client.solrj.response.QueryResponse;
importorg.apache.solr.common.SolrDocumentList;
importorg.apache.solr.common.params.SolrParams;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/test/resources/test-context.xml"})
publicclassSolrServerFactoryTest{
@Autowired
@Qualifier("httpSolrServer")
privateSolrServerhttpSolrServer;
@Autowired
@Qualifier("embeddedSolrServer")
privateSolrServerembeddedSolrServer;
@Test
publicvoiddumy(){
Assert.assertTrue(true);
}
@Test
publicvoidembeddedSolrServerQuery(){
Stringquery="name:DDR";
SolrParamsparams=newSolrQuery(query);
try{
QueryResponseresponse=embeddedSolrServer.query(params);
SolrDocumentListlist=response.getResults();
for(inti=0;i<list.size();i++){
Assert.assertNotNull(list.get(i));
}
}catch(SolrServerExceptione){
e.printStackTrace();
}
}
//@Test
publicvoidhttpSolrServerQuery(){
Stringquery="name:DDR";
SolrParamsparams=newSolrQuery(query);
try{
QueryResponseresponse=httpSolrServer.query(params);
SolrDocumentListlist=response.getResults();
for(inti=0;i<list.size();i++){
Assert.assertNotNull(list.get(i));
}
}catch(SolrServerExceptione){
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