[BAEL-3392] Formatted code examples for [BAEL-3392]

This commit is contained in:
Martin van Wingerden
2019-11-01 09:05:12 +01:00
parent db85c8f275
commit 0e23f2e682
20519 changed files with 1642357 additions and 0 deletions
@@ -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;
}
}
@@ -0,0 +1,56 @@
package com.baeldung.solrjava;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.common.SolrInputDocument;
public class SolrJavaIntegration {
private HttpSolrClient solrClient;
public SolrJavaIntegration(String clientUrl) {
solrClient = new HttpSolrClient.Builder(clientUrl).build();
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();
document.addField("id", documentId);
document.addField("name", itemName);
document.addField("price", itemPrice);
solrClient.add(document);
solrClient.commit();
}
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;
}
protected void setSolrClient(HttpSolrClient solrClient) {
this.solrClient = solrClient;
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,108 @@
package com.baeldung.solrjava;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Before;
import org.junit.Test;
public class SolrJavaLiveTest {
private SolrJavaIntegration solrJavaIntegration;
@Before
public void setUp() throws Exception {
solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore");
solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99");
}
@Test
public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException {
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = null;
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(1, docList.getNumFound());
for (SolrDocument doc : docList) {
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
}
}
@Test
public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException {
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");
QueryResponse response = null;
response = solrJavaIntegration.getSolrClient().query(query);
SolrDocumentList docList = response.getResults();
assertEquals(0, docList.getNumFound());
}
}