Skip to content

Commit 1bfc944

Browse files
nboseckerKevinGilmore
authored andcommitted
Added more indexing/delete/query examples to code (eugenp#1251)
* 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 * More examples for indexing,delete,and query for solrj * More examples for indexing,delete,and query for solrj
1 parent 29882a1 commit 1bfc944

3 files changed

Lines changed: 114 additions & 7 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.solrjava;
2+
3+
import org.apache.solr.client.solrj.beans.Field;
4+
5+
public class ProductBean {
6+
7+
String id;
8+
String name;
9+
String price;
10+
11+
public ProductBean(String id, String name, String price) {
12+
super();
13+
this.id = id;
14+
this.name = name;
15+
this.price = price;
16+
}
17+
18+
public String getId() {
19+
return id;
20+
}
21+
22+
@Field("id")
23+
protected void setId(String id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
@Field("name")
32+
protected void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getPrice() {
37+
return price;
38+
}
39+
40+
@Field("price")
41+
protected void setPrice(String price) {
42+
this.price = price;
43+
}
44+
}

‎apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public SolrJavaIntegration(String clientUrl) {
1717
solrClient.setParser(new XMLResponseParser());
1818
}
1919

20+
public void addProductBean(ProductBean pBean) throws IOException, SolrServerException {
21+
22+
solrClient.addBean(pBean);
23+
solrClient.commit();
24+
}
25+
2026
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {
2127

2228
SolrInputDocument document = new SolrInputDocument();
@@ -27,17 +33,24 @@ public void addSolrDocument(String documentId, String itemName, String itemPrice
2733
solrClient.commit();
2834
}
2935

30-
public void deleteSolrDocument(String documentId) throws SolrServerException, IOException {
36+
public void deleteSolrDocumentById(String documentId) throws SolrServerException, IOException {
3137

3238
solrClient.deleteById(documentId);
3339
solrClient.commit();
3440
}
3541

42+
public void deleteSolrDocumentByQuery(String query) throws SolrServerException, IOException {
43+
44+
solrClient.deleteByQuery(query);
45+
solrClient.commit();
46+
}
47+
3648
protected HttpSolrClient getSolrClient() {
3749
return solrClient;
3850
}
3951

4052
protected void setSolrClient(HttpSolrClient solrClient) {
4153
this.solrClient = solrClient;
4254
}
55+
4356
}

‎apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java‎

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void setUp() throws Exception {
2424
}
2525

2626
@Test
27-
public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
27+
public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException {
2828

2929
SolrQuery query = new SolrQuery();
3030
query.set("q", "id:123456");
@@ -36,15 +36,65 @@ public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
3636
assertEquals(docList.getNumFound(), 1);
3737

3838
for (SolrDocument doc : docList) {
39-
assertEquals((String) doc.getFieldValue("id"), "123456");
40-
assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
39+
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
40+
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
4141
}
4242
}
4343

4444
@Test
45-
public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
45+
public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException {
4646

47-
solrJavaIntegration.deleteSolrDocument("123456");
47+
SolrQuery query = new SolrQuery();
48+
query.set("q", "price:599.99");
49+
QueryResponse response = null;
50+
51+
response = solrJavaIntegration.getSolrClient().query(query);
52+
53+
SolrDocumentList docList = response.getResults();
54+
assertEquals(1, docList.getNumFound());
55+
56+
for (SolrDocument doc : docList) {
57+
assertEquals("123456", (String) doc.getFieldValue("id"));
58+
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
59+
}
60+
}
61+
62+
@Test
63+
public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException {
64+
65+
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456");
66+
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
67+
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
68+
}
69+
70+
@Test
71+
public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException {
72+
73+
ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99");
74+
solrJavaIntegration.addProductBean(pBean);
75+
76+
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888");
77+
assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name"));
78+
assertEquals((Double) 299.99, (Double) doc.getFieldValue("price"));
79+
}
80+
81+
@Test
82+
public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException {
83+
84+
solrJavaIntegration.deleteSolrDocumentById("123456");
85+
86+
SolrQuery query = new SolrQuery();
87+
query.set("q", "id:123456");
88+
QueryResponse response = solrJavaIntegration.getSolrClient().query(query);
89+
90+
SolrDocumentList docList = response.getResults();
91+
assertEquals(0, docList.getNumFound());
92+
}
93+
94+
@Test
95+
public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException {
96+
97+
solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher");
4898

4999
SolrQuery query = new SolrQuery();
50100
query.set("q", "id:123456");
@@ -53,6 +103,6 @@ public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOExcepti
53103
response = solrJavaIntegration.getSolrClient().query(query);
54104

55105
SolrDocumentList docList = response.getResults();
56-
assertEquals(docList.getNumFound(), 0);
106+
assertEquals(0, docList.getNumFound());
57107
}
58108
}

0 commit comments

Comments
 (0)