Search in sources :

Example 6 with QueryResponse

use of com.ibm.watson.developer_cloud.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());
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) Calculation(com.ibm.watson.developer_cloud.discovery.v1.model.Calculation) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 7 with QueryResponse

use of com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method getTrainingDataIsSuccessful.

@Test
public void getTrainingDataIsSuccessful() {
    String naturalLanguageQuery = "Query" + UUID.randomUUID().toString();
    TrainingQuery newQuery = createTestQuery(collectionId, naturalLanguageQuery);
    String queryId = newQuery.getQueryId();
    GetTrainingDataOptions.Builder queryBuilder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery queryResponse = discovery.getTrainingData(queryBuilder.build()).execute();
    assertEquals(queryResponse.getNaturalLanguageQuery(), naturalLanguageQuery);
}
Also used : GetTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 8 with QueryResponse

use of com.ibm.watson.developer_cloud.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());
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 9 with QueryResponse

use of com.ibm.watson.developer_cloud.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());
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 10 with QueryResponse

use of com.ibm.watson.developer_cloud.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);
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) WaitFor(com.ibm.watson.developer_cloud.util.WaitFor) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Ignore(org.junit.Ignore) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)23 Test (org.junit.Test)23 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)20 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)20 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)5 Calculation (com.ibm.watson.developer_cloud.discovery.v1.model.Calculation)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)3 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)3 FederatedQueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.FederatedQueryOptions)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Ignore (org.junit.Ignore)3 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)2 Term (com.ibm.watson.developer_cloud.discovery.v1.model.Term)2 ArrayList (java.util.ArrayList)2 AggregationResult (com.ibm.watson.developer_cloud.discovery.v1.model.AggregationResult)1