From 1bfc944c5a5db228a6bd648794360df6aeb58225 Mon Sep 17 00:00:00 2001 From: Nancy Bosecker Date: Mon, 27 Feb 2017 20:26:30 -0800 Subject: [PATCH] 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 --- .../com/baeldung/solrjava/ProductBean.java | 44 +++++++++++++ .../solrjava/SolrJavaIntegration.java | 15 ++++- .../solrjava/SolrJavaIntegrationTest.java | 62 +++++++++++++++++-- 3 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java new file mode 100644 index 0000000000..14eea8f2f9 --- /dev/null +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java @@ -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; + } +} diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java index f2d21f0993..c55e1c9ada 100644 --- a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java @@ -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; } + } diff --git a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index 22f9eae8ee..7f4599a91d 100644 --- a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -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()); } }