Refactor ElasticSearchUnitTest

This commit is contained in:
pivovarit 2017-05-15 22:17:20 +03:00
parent 1130dc6b1e
commit 94e7cfd94b

View File

@ -1,14 +1,6 @@
package com.baeldung.elasticsearch; package com.baeldung.elasticsearch;
import static org.elasticsearch.node.NodeBuilder.*; import com.alibaba.fastjson.JSON;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
@ -22,12 +14,19 @@ import org.elasticsearch.search.SearchHit;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.alibaba.fastjson.JSON; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ElasticSearchUnitTest { public class ElasticSearchUnitTest {
private List<Person> listOfPersons = new ArrayList<Person>(); private List<Person> listOfPersons = new ArrayList<Person>();
String jsonString = null; private Client client = null;
Client client = null;
@Before @Before
public void setUp() { public void setUp() {
@ -35,15 +34,20 @@ public class ElasticSearchUnitTest {
Person person2 = new Person(25, "Janette Doe", new Date()); Person person2 = new Person(25, "Janette Doe", new Date());
listOfPersons.add(person1); listOfPersons.add(person1);
listOfPersons.add(person2); listOfPersons.add(person2);
jsonString = JSON.toJSONString(listOfPersons); Node node = nodeBuilder()
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node(); .clusterName("elasticsearch")
.client(true)
.node();
client = node.client(); client = node.client();
} }
@Test @Test
public void givenJsonString_whenJavaObject_thenIndexDocument() { public void givenJsonString_whenJavaObject_thenIndexDocument() {
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
IndexResponse response = client.prepareIndex("people", "Doe").setSource(jsonObject).get(); IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject)
.get();
String index = response.getIndex(); String index = response.getIndex();
String type = response.getType(); String type = response.getType();
assertTrue(response.isCreated()); assertTrue(response.isCreated());
@ -54,17 +58,27 @@ public class ElasticSearchUnitTest {
@Test @Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() { public void givenDocumentId_whenJavaObject_thenDeleteDocument() {
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
IndexResponse response = client.prepareIndex("people", "Doe").setSource(jsonObject).get(); IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(jsonObject)
.get();
String id = response.getId(); String id = response.getId();
DeleteResponse deleteResponse = client.prepareDelete("people", "Doe", id).get(); DeleteResponse deleteResponse = client
.prepareDelete("people", "Doe", id)
.get();
assertTrue(deleteResponse.isFound()); assertTrue(deleteResponse.isFound());
} }
@Test @Test
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { public void givenSearchRequest_whenMatchAll_thenReturnAllResults() {
SearchResponse response = client.prepareSearch().execute().actionGet(); SearchResponse response = client
SearchHit[] searchHits = response.getHits().getHits(); .prepareSearch()
List<Person> results = new ArrayList<Person>(); .execute()
.actionGet();
SearchHit[] searchHits = response
.getHits()
.getHits();
List<Person> results = new ArrayList<>();
for (SearchHit hit : searchHits) { for (SearchHit hit : searchHits) {
String sourceAsString = hit.getSourceAsString(); String sourceAsString = hit.getSourceAsString();
Person person = JSON.parseObject(sourceAsString, Person.class); Person person = JSON.parseObject(sourceAsString, Person.class);
@ -73,29 +87,64 @@ public class ElasticSearchUnitTest {
} }
@Test @Test
public void givenSearchParamters_thenReturnResults() { public void givenSearchParameters_thenReturnResults() {
boolean isExecutedSuccessfully = true; SearchResponse response = client
SearchResponse response = client.prepareSearch().setTypes().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setPostFilter(QueryBuilders.rangeQuery("age").from(5).to(15)).setFrom(0).setSize(60).setExplain(true).execute().actionGet(); .prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders
.rangeQuery("age")
.from(5)
.to(15))
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
SearchResponse response2 = client.prepareSearch().setTypes().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")).setFrom(0).setSize(60).setExplain(true).execute().actionGet(); SearchResponse response2 = client
.prepareSearch()
.setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"))
.setFrom(0)
.setSize(60)
.setExplain(true)
.execute()
.actionGet();
SearchResponse response3 = client.prepareSearch().setTypes().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setPostFilter(QueryBuilders.matchQuery("John", "Name*")).setFrom(0).setSize(60).setExplain(true).execute().actionGet(); SearchResponse response3 = client
try { .prepareSearch()
response2.getHits(); .setTypes()
response3.getHits(); .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
List<SearchHit> searchHits = Arrays.asList(response.getHits().getHits()); .setPostFilter(QueryBuilders.matchQuery("John", "Name*"))
final List<Person> results = new ArrayList<Person>(); .setFrom(0)
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class))); .setSize(60)
} catch (Exception e) { .setExplain(true)
isExecutedSuccessfully = false; .execute()
} .actionGet();
assertTrue(isExecutedSuccessfully); response2.getHits();
response3.getHits();
List<SearchHit> searchHits = Arrays.asList(response
.getHits()
.getHits());
final List<Person> results = new ArrayList<>();
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class)));
} }
@Test @Test
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test").field("salary", "11500").field("age", "10").endObject(); XContentBuilder builder = XContentFactory
IndexResponse response = client.prepareIndex("people", "Doe").setSource(builder).get(); .jsonBuilder()
.startObject()
.field("fullName", "Test")
.field("salary", "11500")
.field("age", "10")
.endObject();
IndexResponse response = client
.prepareIndex("people", "Doe")
.setSource(builder)
.get();
assertTrue(response.isCreated()); assertTrue(response.isCreated());
} }
} }