Skip to content

Commit 3371c90

Browse files
nboseckerKevinGilmore
authored andcommitted
Moved code to apache-solrj from spring-data-solr, updated code to 6.4.0 solrj, fixed dependencies in both modules (eugenp#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
1 parent fda362f commit 3371c90

5 files changed

Lines changed: 100 additions & 24 deletions

File tree

‎apache-solrj/pom.xml‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-solrj</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>apache-solrj</name>
9+
10+
<properties>
11+
<junit.version>4.12</junit.version>
12+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.apache.solr</groupId>
18+
<artifactId>solr-solrj</artifactId>
19+
<version>6.4.0</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>${junit.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<version>2.3.2</version>
34+
</plugin>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-surefire-plugin</artifactId>
38+
<version>${maven-surefire-plugin.version}</version>
39+
<configuration>
40+
<source>1.8</source>
41+
<target>1.8</target>
42+
<excludes>
43+
<exclude>**/*IntegrationTest.java</exclude>
44+
<exclude>**/*LiveTest.java</exclude>
45+
</excludes>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.solrjava;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.solr.client.solrj.SolrServerException;
6+
import org.apache.solr.client.solrj.impl.HttpSolrClient;
7+
import org.apache.solr.client.solrj.impl.XMLResponseParser;
8+
import org.apache.solr.common.SolrInputDocument;
9+
10+
public class SolrJavaIntegration {
11+
12+
private HttpSolrClient solrClient;
13+
14+
public SolrJavaIntegration(String clientUrl) {
15+
16+
solrClient = new HttpSolrClient.Builder(clientUrl).build();
17+
solrClient.setParser(new XMLResponseParser());
18+
}
19+
20+
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {
21+
22+
SolrInputDocument document = new SolrInputDocument();
23+
document.addField("id", documentId);
24+
document.addField("name", itemName);
25+
document.addField("price", itemPrice);
26+
solrClient.add(document);
27+
solrClient.commit();
28+
}
29+
30+
public void deleteSolrDocument(String documentId) throws SolrServerException, IOException {
31+
32+
solrClient.deleteById(documentId);
33+
solrClient.commit();
34+
}
35+
36+
protected HttpSolrClient getSolrClient() {
37+
return solrClient;
38+
}
39+
40+
protected void setSolrClient(HttpSolrClient solrClient) {
41+
this.solrClient = solrClient;
42+
}
43+
}

spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java renamed to apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,21 @@
66

77
import org.apache.solr.client.solrj.SolrQuery;
88
import org.apache.solr.client.solrj.SolrServerException;
9-
import org.apache.solr.client.solrj.impl.HttpSolrClient;
10-
import org.apache.solr.client.solrj.impl.XMLResponseParser;
119
import org.apache.solr.client.solrj.response.QueryResponse;
1210
import org.apache.solr.common.SolrDocument;
1311
import org.apache.solr.common.SolrDocumentList;
14-
import org.apache.solr.common.SolrInputDocument;
1512
import org.junit.Before;
1613
import org.junit.Test;
1714

1815
public class SolrJavaIntegrationTest {
1916

20-
private HttpSolrClient solr;
21-
private SolrInputDocument document;
17+
private SolrJavaIntegration solrJavaIntegration;
2218

2319
@Before
2420
public void setUp() throws Exception {
2521

26-
solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore");
27-
solr.setParser(new XMLResponseParser());
28-
29-
document = new SolrInputDocument();
30-
document.addField("id", "123456");
31-
document.addField("name", "Kenmore Dishwasher");
32-
document.addField("price", "599.99");
33-
solr.add(document);
34-
solr.commit();
22+
solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore");
23+
solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99");
3524
}
3625

3726
@Test
@@ -41,7 +30,7 @@ public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
4130
query.set("q", "id:123456");
4231
QueryResponse response = null;
4332

44-
response = solr.query(query);
33+
response = solrJavaIntegration.getSolrClient().query(query);
4534

4635
SolrDocumentList docList = response.getResults();
4736
assertEquals(docList.getNumFound(), 1);
@@ -55,14 +44,13 @@ public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
5544
@Test
5645
public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
5746

58-
solr.deleteById("123456");
59-
solr.commit();
47+
solrJavaIntegration.deleteSolrDocument("123456");
6048

6149
SolrQuery query = new SolrQuery();
6250
query.set("q", "id:123456");
6351
QueryResponse response = null;
6452

65-
response = solr.query(query);
53+
response = solrJavaIntegration.getSolrClient().query(query);
6654

6755
SolrDocumentList docList = response.getResults();
6856
assertEquals(docList.getNumFound(), 0);

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194

195195
<module>struts2</module>
196196
<module>apache-velocity</module>
197+
<module>apache-solrj</module>
197198

198199
</modules>
199200

‎spring-data-solr/pom.xml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@
5151
<version>${spring.version}</version>
5252
<scope>test</scope>
5353
</dependency>
54-
<!-- This is included in spring-data-solr above, no need to include twice
55-
<dependency>
56-
<groupId>org.apache.solr</groupId>
57-
<artifactId>solr-solrj</artifactId>
58-
<version>5.5.0</version>
59-
</dependency-->
6054
</dependencies>
6155

6256
<build>

0 commit comments

Comments
 (0)