Add new QueryBuilder cases, factorize node client initialisation
This commit is contained in:
parent
ec0eb9c804
commit
6ecf4f6e78
|
@ -26,6 +26,8 @@ import com.alibaba.fastjson.JSON;
|
|||
public class ElasticSearchUnitTests {
|
||||
private List<Person> listOfPersons = new ArrayList<Person>();
|
||||
String jsonString = null;
|
||||
|
||||
Client client = null;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
@ -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,19 +85,42 @@ public class ElasticSearchUnitTests {
|
|||
.setFrom(0).setSize(60).setExplain(true)
|
||||
.execute()
|
||||
.actionGet();
|
||||
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);
|
||||
|
||||
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){
|
||||
String sourceAsString = hit.getSourceAsString();
|
||||
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