Added more indexing/delete/query examples to code (#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
This commit is contained in:
Nancy Bosecker 2017-02-27 20:26:30 -08:00 committed by KevinGilmore
parent 29882a1f06
commit 1bfc944c5a
3 changed files with 114 additions and 7 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.solrjava;
import org.apache.solr.client.solrj.beans.Field;
public class ProductBean {
String id;
String name;
String price;
public ProductBean(String id, String name, String price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
@Field("id")
protected void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@Field("name")
protected void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
@Field("price")
protected void setPrice(String price) {
this.price = price;
}
}

View File

@ -17,6 +17,12 @@ public class SolrJavaIntegration {
solrClient.setParser(new XMLResponseParser());
}
public void addProductBean(ProductBean pBean) throws IOException, SolrServerException {
solrClient.addBean(pBean);
solrClient.commit();
}
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {
SolrInputDocument document = new SolrInputDocument();
@ -27,12 +33,18 @@ public class SolrJavaIntegration {
solrClient.commit();
}
public void deleteSolrDocument(String documentId) throws SolrServerException, IOException {
public void deleteSolrDocumentById(String documentId) throws SolrServerException, IOException {
solrClient.deleteById(documentId);
solrClient.commit();
}
public void deleteSolrDocumentByQuery(String query) throws SolrServerException, IOException {
solrClient.deleteByQuery(query);
solrClient.commit();
}
protected HttpSolrClient getSolrClient() {
return solrClient;
}
@ -40,4 +52,5 @@ public class SolrJavaIntegration {
protected void setSolrClient(HttpSolrClient solrClient) {
this.solrClient = solrClient;
}
}

View File

@ -24,7 +24,7 @@ public class SolrJavaIntegrationTest {
}
@Test
public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException {
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
@ -36,15 +36,65 @@ public class SolrJavaIntegrationTest {
assertEquals(docList.getNumFound(), 1);
for (SolrDocument doc : docList) {
assertEquals((String) doc.getFieldValue("id"), "123456");
assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
}
}
@Test
public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException {
solrJavaIntegration.deleteSolrDocument("123456");
SolrQuery query = new SolrQuery();
query.set("q", "price:599.99");
QueryResponse response = null;
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(1, docList.getNumFound());
for (SolrDocument doc : docList) {
assertEquals("123456", (String) doc.getFieldValue("id"));
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
}
}
@Test
public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException {
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456");
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
}
@Test
public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException {
ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99");
solrJavaIntegration.addProductBean(pBean);
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888");
assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name"));
assertEquals((Double) 299.99, (Double) doc.getFieldValue("price"));
}
@Test
public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException {
solrJavaIntegration.deleteSolrDocumentById("123456");
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(0, docList.getNumFound());
}
@Test
public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException {
solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher");
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
@ -53,6 +103,6 @@ public class SolrJavaIntegrationTest {
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 0);
assertEquals(0, docList.getNumFound());
}
}