use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithAggregationAverageIsSuccessful.
@Test
public void queryWithAggregationAverageIsSuccessful() throws InterruptedException {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
StringBuilder sb = new StringBuilder();
sb.append(AggregationType.AVERAGE);
sb.append(Operator.OPENING_GROUPING);
sb.append("field");
sb.append(Operator.CLOSING_GROUPING);
String aggregation = sb.toString();
queryBuilder.aggregation(aggregation);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
Calculation avg = (Calculation) queryResponse.getAggregations().get(0);
assertEquals(AggregationType.AVERAGE.getName(), avg.getType());
assertEquals(new Double(4.5), avg.getValue());
}
use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithFilterIsSuccessful.
@Test
public void queryWithFilterIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.filter("field" + Operator.CONTAINS + 1);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
assertEquals(new Long(1), queryResponse.getMatchingResults());
assertEquals(1, queryResponse.getResults().size());
}
use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithReturnFieldsIsSuccessful.
@Test
public void queryWithReturnFieldsIsSuccessful() {
String collectionId = setupTestDocuments();
createTestDocument("{\"field_2\":\"value_2\"}", collectionId);
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
List<String> fieldNames = new ArrayList<>();
fieldNames.add("field");
queryBuilder.returnFields(fieldNames);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
String[] expected = new String[] { "id", "result_metadata", "field" };
assertTrue(queryResponse.getResults().get(0).keySet().containsAll(Arrays.asList(expected)));
}
use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithAggregationMinimumIsSuccessful.
@Test
public void queryWithAggregationMinimumIsSuccessful() throws InterruptedException {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
StringBuilder sb = new StringBuilder();
sb.append(AggregationType.MIN);
sb.append(Operator.OPENING_GROUPING);
sb.append("field");
sb.append(Operator.CLOSING_GROUPING);
String aggregation = sb.toString();
queryBuilder.aggregation(aggregation);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
Calculation min = (Calculation) queryResponse.getAggregations().get(0);
assertEquals(AggregationType.MIN.getName(), min.getType());
assertEquals(new Double(0), min.getValue());
}
use of com.ibm.watson.discovery.v2.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithAggregationTermIsSuccessful.
@Test
public void queryWithAggregationTermIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
StringBuilder sb = new StringBuilder();
sb.append(AggregationType.TERM);
sb.append(Operator.OPENING_GROUPING);
sb.append("field");
sb.append(Operator.AND);
sb.append(10L);
sb.append(Operator.CLOSING_GROUPING);
String aggregation = sb.toString();
queryBuilder.aggregation(aggregation);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
Term term = (Term) queryResponse.getAggregations().get(0);
assertEquals(1, queryResponse.getAggregations().size());
assertEquals(new Long(10), term.getCount());
}
Aggregations