use of com.ibm.watson.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithAggregationMaximumIsSuccessful.
@Test
public void queryWithAggregationMaximumIsSuccessful() throws InterruptedException {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
StringBuilder sb = new StringBuilder();
sb.append(AggregationType.MAX);
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 max = (Calculation) queryResponse.getAggregations().get(0);
assertEquals(AggregationType.MAX.getName(), max.getType());
assertEquals(new Double(9), max.getValue());
}
use of com.ibm.watson.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithCountIsSuccessful.
// query tests
@Test
public void queryWithCountIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.count(5L);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
assertEquals(new Long(10), queryResponse.getMatchingResults());
assertEquals(5, queryResponse.getResults().size());
}
use of com.ibm.watson.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method queryWithOffsetIsSuccessful.
@Test
public void queryWithOffsetIsSuccessful() {
String collectionId = setupTestDocuments();
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.offset(5L);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
assertEquals(new Long(10), queryResponse.getMatchingResults());
assertEquals(5, queryResponse.getResults().size());
}
use of com.ibm.watson.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method addDocumentWithMetadataIsSuccessful.
@Ignore
@SuppressWarnings("deprecation")
@Test
public void addDocumentWithMetadataIsSuccessful() {
Collection collection = createTestCollection();
String collectionId = collection.getCollectionId();
String myDocumentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
JsonObject myMetadata = new JsonObject();
myMetadata.add("foo", new JsonPrimitive("bar"));
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
builder.filename("test_file");
builder.metadata(myMetadata.toString());
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute();
WaitFor.Condition documentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, createResponse.getDocumentId());
WaitFor.waitFor(documentAccepted, 5, TimeUnit.SECONDS, 500);
QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId).build();
QueryResponse queryResponse = discovery.query(queryOptions).execute();
assertTrue(queryResponse.getResults().get(0).getMetadata() != null);
}
use of com.ibm.watson.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method queryIsSuccessful.
// Query tests
@Test
public void queryIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(queryResp));
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.count(5L);
queryBuilder.offset(5L);
List<String> fieldNames = new ArrayList<>();
fieldNames.add("field");
queryBuilder.returnFields(fieldNames);
queryBuilder.query("field" + Operator.CONTAINS + 1);
queryBuilder.filter("field" + Operator.CONTAINS + 1);
queryBuilder.similar(true);
queryBuilder.similarDocumentIds(Arrays.asList("doc1", "doc2"));
queryBuilder.similarFields(Arrays.asList("field1", "field2"));
QueryResponse response = discoveryService.query(queryBuilder.build()).execute();
RecordedRequest request = server.takeRequest();
assertEquals(Q1_PATH + "&similar=true&similar.document_ids=doc1,doc2&similar.fields=field1,field2", request.getPath());
assertEquals(GET, request.getMethod());
assertEquals(GsonSingleton.getGson().toJsonTree(queryResp), GsonSingleton.getGson().toJsonTree(response));
}
Aggregations