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:
parent
29882a1f06
commit
1bfc944c5a
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,12 @@ public class SolrJavaIntegration {
|
||||||
solrClient.setParser(new XMLResponseParser());
|
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 {
|
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {
|
||||||
|
|
||||||
SolrInputDocument document = new SolrInputDocument();
|
SolrInputDocument document = new SolrInputDocument();
|
||||||
|
@ -27,12 +33,18 @@ public class SolrJavaIntegration {
|
||||||
solrClient.commit();
|
solrClient.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteSolrDocument(String documentId) throws SolrServerException, IOException {
|
public void deleteSolrDocumentById(String documentId) throws SolrServerException, IOException {
|
||||||
|
|
||||||
solrClient.deleteById(documentId);
|
solrClient.deleteById(documentId);
|
||||||
solrClient.commit();
|
solrClient.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteSolrDocumentByQuery(String query) throws SolrServerException, IOException {
|
||||||
|
|
||||||
|
solrClient.deleteByQuery(query);
|
||||||
|
solrClient.commit();
|
||||||
|
}
|
||||||
|
|
||||||
protected HttpSolrClient getSolrClient() {
|
protected HttpSolrClient getSolrClient() {
|
||||||
return solrClient;
|
return solrClient;
|
||||||
}
|
}
|
||||||
|
@ -40,4 +52,5 @@ public class SolrJavaIntegration {
|
||||||
protected void setSolrClient(HttpSolrClient solrClient) {
|
protected void setSolrClient(HttpSolrClient solrClient) {
|
||||||
this.solrClient = solrClient;
|
this.solrClient = solrClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class SolrJavaIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException {
|
public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException {
|
||||||
|
|
||||||
SolrQuery query = new SolrQuery();
|
SolrQuery query = new SolrQuery();
|
||||||
query.set("q", "id:123456");
|
query.set("q", "id:123456");
|
||||||
|
@ -36,15 +36,65 @@ public class SolrJavaIntegrationTest {
|
||||||
assertEquals(docList.getNumFound(), 1);
|
assertEquals(docList.getNumFound(), 1);
|
||||||
|
|
||||||
for (SolrDocument doc : docList) {
|
for (SolrDocument doc : docList) {
|
||||||
assertEquals((String) doc.getFieldValue("id"), "123456");
|
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
|
||||||
assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
|
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
SolrQuery query = new SolrQuery();
|
||||||
query.set("q", "id:123456");
|
query.set("q", "id:123456");
|
||||||
|
@ -53,6 +103,6 @@ public class SolrJavaIntegrationTest {
|
||||||
response = solrJavaIntegration.getSolrClient().query(query);
|
response = solrJavaIntegration.getSolrClient().query(query);
|
||||||
|
|
||||||
SolrDocumentList docList = response.getResults();
|
SolrDocumentList docList = response.getResults();
|
||||||
assertEquals(docList.getNumFound(), 0);
|
assertEquals(0, docList.getNumFound());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue