From 3371c9047a3d7d704ff0583dd4551a40be89183e Mon Sep 17 00:00:00 2001 From: Nancy Bosecker Date: Mon, 20 Feb 2017 17:28:16 -0800 Subject: [PATCH] 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 --- apache-solrj/pom.xml | 50 +++++++++++++++++++ .../solrjava/SolrJavaIntegration.java | 43 ++++++++++++++++ .../solrjava/SolrJavaIntegrationTest.java | 24 +++------ pom.xml | 1 + spring-data-solr/pom.xml | 6 --- 5 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 apache-solrj/pom.xml create mode 100644 apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java rename {spring-data-solr => apache-solrj}/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java (64%) diff --git a/apache-solrj/pom.xml b/apache-solrj/pom.xml new file mode 100644 index 0000000000..74daeae55c --- /dev/null +++ b/apache-solrj/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + com.baeldung + apache-solrj + 0.0.1-SNAPSHOT + jar + apache-solrj + + + 4.12 + 2.19.1 + + + + + org.apache.solr + solr-solrj + 6.4.0 + + + junit + junit + ${junit.version} + test + + + + + + + maven-compiler-plugin + 2.3.2 + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + 1.8 + 1.8 + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + \ No newline at end of file diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java new file mode 100644 index 0000000000..f2d21f0993 --- /dev/null +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java @@ -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; + } +} diff --git a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java similarity index 64% rename from spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java rename to apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index ce90ccaf16..22f9eae8ee 100644 --- a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -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); diff --git a/pom.xml b/pom.xml index 82df776044..aa58b1d2e9 100644 --- a/pom.xml +++ b/pom.xml @@ -194,6 +194,7 @@ struts2 apache-velocity + apache-solrj diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml index 2aa9f86a96..e43b3ff774 100644 --- a/spring-data-solr/pom.xml +++ b/spring-data-solr/pom.xml @@ -51,12 +51,6 @@ ${spring.version} test -