Add new QueryBuilder cases, factorize node client initialisation
This commit is contained in:
parent
ec0eb9c804
commit
6ecf4f6e78
|
@ -27,6 +27,8 @@ public class ElasticSearchUnitTests {
|
|||
private List<Person> listOfPersons = new ArrayList<Person>();
|
||||
String jsonString = null;
|
||||
|
||||
Client client = null;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Person person1 = new Person(10, "John Doe", new Date());
|
||||
|
@ -34,15 +36,13 @@ public class ElasticSearchUnitTests {
|
|||
listOfPersons.add(person1);
|
||||
listOfPersons.add(person2);
|
||||
jsonString = JSON.toJSONString(listOfPersons);
|
||||
System.out.println(jsonString);
|
||||
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
|
||||
client = node.client();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsonString_whenJavaObject_thenIndexDocument() {
|
||||
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")
|
||||
.setSource(jsonObject).get();
|
||||
String id = response.getId();
|
||||
|
@ -56,9 +56,6 @@ public class ElasticSearchUnitTests {
|
|||
@Test
|
||||
public void givenDocumentId_whenJavaObject_thenDeleteDocument() {
|
||||
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")
|
||||
.setSource(jsonObject).get();
|
||||
String id = response.getId();
|
||||
|
@ -68,8 +65,6 @@ public class ElasticSearchUnitTests {
|
|||
|
||||
@Test
|
||||
public void givenSearchRequest_whenMatchAll_thenReturnAllResults() {
|
||||
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
|
||||
Client client = node.client();
|
||||
SearchResponse response = client.prepareSearch().execute().actionGet();
|
||||
SearchHit[] searchHits = response.getHits().getHits();
|
||||
List<Person> results = new ArrayList<Person>();
|
||||
|
@ -82,8 +77,7 @@ public class ElasticSearchUnitTests {
|
|||
|
||||
@Test
|
||||
public void givenSearchParamters_thenReturnResults() {
|
||||
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
|
||||
Client client = node.client();
|
||||
boolean isExecutedSuccessfully = true;
|
||||
SearchResponse response = client.prepareSearch()
|
||||
.setTypes()
|
||||
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
||||
|
@ -91,6 +85,27 @@ public class ElasticSearchUnitTests {
|
|||
.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();
|
||||
try {
|
||||
response2.getHits();
|
||||
response3.getHits();
|
||||
SearchHit[] searchHits = response.getHits().getHits();
|
||||
List<Person> results = new ArrayList<Person>();
|
||||
for(SearchHit hit : searchHits){
|
||||
|
@ -98,12 +113,14 @@ public class ElasticSearchUnitTests {
|
|||
Person person = JSON.parseObject(sourceAsString, Person.class);
|
||||
results.add(person);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
isExecutedSuccessfully = false;
|
||||
}
|
||||
assertTrue(isExecutedSuccessfully);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
|
||||
Node node = nodeBuilder().clusterName("elasticsearch").client(true).node();
|
||||
Client client = node.client();
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("fullName", "Test")
|
||||
|
|
Loading…
Reference in New Issue