Solr w Apache SolrJ (#1073)

* Solr w Apache SolrJ

* Solr w Apache SolrJ
This commit is contained in:
Nancy Bosecker 2017-02-15 15:48:53 -08:00 committed by KevinGilmore
parent 41ea1898eb
commit a4c5fb1c12
2 changed files with 76 additions and 0 deletions

View File

@ -51,6 +51,12 @@
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- This is included in spring-data-solr above, no need to include twice
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.5.0</version>
</dependency-->
</dependencies>
<build>

View File

@ -0,0 +1,70 @@
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.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test;
public class SolrJavaIntegrationTest {
private HttpSolrClient solr;
@Before
public void setUp() throws Exception {
solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore");
solr.setParser(new XMLResponseParser());
}
@Test
public void givenAdd_thenVerifyAdded() throws SolrServerException, IOException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "123456");
document.addField("name", "Kenmore Dishwasher");
document.addField("price", "599.99");
solr.add(document);
solr.commit();
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = null;
response = solr.query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 1);
for (SolrDocument doc : docList) {
assertEquals((String) doc.getFieldValue("id"), "123456");
assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99);
}
}
@Test
public void givenDelete_thenVerifyDeleted() throws SolrServerException, IOException {
solr.deleteById("123456");
solr.commit();
SolrQuery query = new SolrQuery();
query.set("q", "id:123456");
QueryResponse response = null;
response = solr.query(query);
SolrDocumentList docList = response.getResults();
assertEquals(docList.getNumFound(), 0);
}
}