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