Add new QueryBuilder cases, factorize node client initialisation

This commit is contained in:
GuenHamza 2016-09-16 00:25:27 +01:00 committed by GitHub
parent ec0eb9c804
commit 6ecf4f6e78
1 changed files with 36 additions and 19 deletions

View File

@ -26,6 +26,8 @@ import com.alibaba.fastjson.JSON;
public class ElasticSearchUnitTests { public class ElasticSearchUnitTests {
private List<Person> listOfPersons = new ArrayList<Person>(); private List<Person> listOfPersons = new ArrayList<Person>();
String jsonString = null; String jsonString = null;
Client client = null;
@Before @Before
public void setUp() { public void setUp() {
@ -34,15 +36,13 @@ public class ElasticSearchUnitTests {
listOfPersons.add(person1); listOfPersons.add(person1);
listOfPersons.add(person2); listOfPersons.add(person2);
jsonString = JSON.toJSONString(listOfPersons); jsonString = JSON.toJSONString(listOfPersons);
System.out.println(jsonString); Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
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\"}";
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
Client client = node.client();
IndexResponse response = client.prepareIndex("people", "Doe") IndexResponse response = client.prepareIndex("people", "Doe")
.setSource(jsonObject).get(); .setSource(jsonObject).get();
String id = response.getId(); String id = response.getId();
@ -56,9 +56,6 @@ public class ElasticSearchUnitTests {
@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\"}";
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
Client client = node.client();
IndexResponse response = client.prepareIndex("people", "Doe") IndexResponse response = client.prepareIndex("people", "Doe")
.setSource(jsonObject).get(); .setSource(jsonObject).get();
String id = response.getId(); String id = response.getId();
@ -68,8 +65,6 @@ public class ElasticSearchUnitTests {
@Test @Test
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { public void givenSearchRequest_whenMatchAll_thenReturnAllResults() {
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
Client client = node.client();
SearchResponse response = client.prepareSearch().execute().actionGet(); SearchResponse response = client.prepareSearch().execute().actionGet();
SearchHit[] searchHits = response.getHits().getHits(); SearchHit[] searchHits = response.getHits().getHits();
List<Person> results = new ArrayList<Person>(); List<Person> results = new ArrayList<Person>();
@ -82,8 +77,7 @@ public class ElasticSearchUnitTests {
@Test @Test
public void givenSearchParamters_thenReturnResults() { public void givenSearchParamters_thenReturnResults() {
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node(); boolean isExecutedSuccessfully = true;
Client client = node.client();
SearchResponse response = client.prepareSearch() SearchResponse response = client.prepareSearch()
.setTypes() .setTypes()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
@ -91,19 +85,42 @@ public class ElasticSearchUnitTests {
.setFrom(0).setSize(60).setExplain(true) .setFrom(0).setSize(60).setExplain(true)
.execute() .execute()
.actionGet(); .actionGet();
SearchHit[] searchHits = response.getHits().getHits();
List<Person> results = new ArrayList<Person>(); SearchResponse response2 = client.prepareSearch()
for(SearchHit hit : searchHits){ .setTypes()
String sourceAsString = hit.getSourceAsString(); .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
Person person = JSON.parseObject(sourceAsString, Person.class); .setPostFilter(QueryBuilders
results.add(person); .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();
try {
response2.getHits();
response3.getHits();
SearchHit[] searchHits = response.getHits().getHits();
List<Person> results = new ArrayList<Person>();
for(SearchHit hit : searchHits){
String sourceAsString = hit.getSourceAsString();
Person person = JSON.parseObject(sourceAsString, Person.class);
results.add(person);
}
} catch (Exception e) {
isExecutedSuccessfully = false;
} }
assertTrue(isExecutedSuccessfully);
} }
@Test @Test
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
Client client = node.client();
XContentBuilder builder = XContentFactory.jsonBuilder() XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject() .startObject()
.field("fullName", "Test") .field("fullName", "Test")