Moved code to apache-solrj from spring-data-solr, updated code to 6.4.0 solrj, fixed dependencies in both modules (#1196)

* Solr w Apache SolrJ

* Solr w Apache SolrJ

* updated test names and moved add to @before method

* create apache-solrj module, moved code from spring-data-solr
This commit is contained in:
Nancy Bosecker 2017-02-20 17:28:16 -08:00 committed by KevinGilmore
parent fda362f79d
commit 3371c9047a
5 changed files with 100 additions and 24 deletions

50
apache-solrj/pom.xml Normal file
View File

@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-solrj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>apache-solrj</name>
<properties>
<junit.version>4.12</junit.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>6.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,43 @@
package com.baeldung.solrjava;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.common.SolrInputDocument;
public class SolrJavaIntegration {
private HttpSolrClient solrClient;
public SolrJavaIntegration(String clientUrl) {
solrClient = new HttpSolrClient.Builder(clientUrl).build();
solrClient.setParser(new XMLResponseParser());
}
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", documentId);
document.addField("name", itemName);
document.addField("price", itemPrice);
solrClient.add(document);
solrClient.commit();
}
public void deleteSolrDocument(String documentId) throws SolrServerException, IOException {
solrClient.deleteById(documentId);
solrClient.commit();
}
protected HttpSolrClient getSolrClient() {
return solrClient;
}
protected void setSolrClient(HttpSolrClient solrClient) {
this.solrClient = solrClient;
}
}

View File

@ -6,32 +6,21 @@ import java.io.IOException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test;
public class SolrJavaIntegrationTest {
private HttpSolrClient solr;
private SolrInputDocument document;
private SolrJavaIntegration solrJavaIntegration;
@Before
public void setUp() throws Exception {
solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore");
solr.setParser(new XMLResponseParser());
document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("name", "Kenmore Dishwasher");
document.addField("price", "599.99");
solr.add(document);
solr.commit();
solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore");
solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99");
}
@Test
@ -41,7 +30,7 @@ public class SolrJavaIntegrationTest {
query.set("q", "id:123456");
QueryResponse response = null;
response = solr.query(query);
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 1);
@ -55,14 +44,13 @@ public class SolrJavaIntegrationTest {
@Test
public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
solr.deleteById("123456");
solr.commit();
solrJavaIntegration.deleteSolrDocument("123456");
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = null;
response = solr.query(query);
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 0);

View File

@ -194,6 +194,7 @@
<module>struts2</module>
<module>apache-velocity</module>
<module>apache-solrj</module>
</modules>

View File

@ -51,12 +51,6 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- This is included in spring-data-solr above, no need to include twice
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.5.0</version>
</dependency-->
</dependencies>
<build>