Search in sources :

Example 21 with QueryResponse

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

the class DiscoveryServiceIT method queryWithNestedAggregationTermIsSuccessful.

@Test
public void queryWithNestedAggregationTermIsSuccessful() {
    Collection collection = createTestCollection();
    String collectionId = collection.getCollectionId();
    createTestDocument("test_document_1", collectionId);
    createTestDocument("test_document_2", collectionId);
    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.CLOSING_GROUPING);
    sb.append(Operator.NEST_AGGREGATION);
    sb.append(AggregationType.TERM);
    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();
    Term term = (Term) queryResponse.getAggregations().get(0);
    AggregationResult agResults = term.getResults().get(0);
    List<QueryAggregation> aggregations = agResults.getAggregations();
    assertFalse(aggregations.isEmpty());
}
Also used : AggregationResult(com.ibm.watson.developer_cloud.discovery.v1.model.AggregationResult) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Term(com.ibm.watson.developer_cloud.discovery.v1.model.Term) QueryAggregation(com.ibm.watson.developer_cloud.discovery.v1.model.QueryAggregation) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 22 with QueryResponse

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

the class DiscoveryServiceIT method issueNumber654.

@Test
public void issueNumber654() {
    String collectionId = setupTestDocuments();
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:1|3");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
    assertEquals(new Long(2), queryResponse.getMatchingResults());
    assertEquals(2, 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 23 with QueryResponse

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

the class DiscoveryServiceIT method updateDocumentWithMetadataIsSuccessful.

@Test
@Ignore("Pending implementation of 'processing' after document update")
public void updateDocumentWithMetadataIsSuccessful() {
    Collection collection = createTestCollection();
    String collectionId = collection.getCollectionId();
    DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
    String myDocumentJson = "{\"field\":\"value2\"}";
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentAccepted.getDocumentId());
    updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    updateBuilder.metadata(myMetadata.toString());
    DocumentAccepted updateResponse = discovery.updateDocument(updateBuilder.build()).execute();
    WaitFor.Condition waitForDocumentAccepted = new WaitForDocumentAccepted(environmentId, collectionId, updateResponse.getDocumentId());
    WaitFor.waitFor(waitForDocumentAccepted, 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) UpdateDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) 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)

Example 24 with QueryResponse

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

the class DiscoveryServiceIT method queryWithPassagesIsSuccessful.

@Test
public void queryWithPassagesIsSuccessful() throws InterruptedException, FileNotFoundException {
    Collection testCollection = createTestCollection();
    String collectionId = testCollection.getCollectionId();
    createTestDocument(getStringFromInputStream(new FileInputStream(PASSAGES_TEST_FILE_1)), "test_document_1", collectionId);
    createTestDocument(getStringFromInputStream(new FileInputStream(PASSAGES_TEST_FILE_2)), "test_document_2", collectionId);
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.passages(true);
    queryBuilder.naturalLanguageQuery("Watson");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
    List<QueryPassages> passages = queryResponse.getPassages();
    assertTrue(passages.size() > 0);
    for (QueryPassages passage : passages) {
        assertTrue(passage.getPassageText().contains("Watson"));
    }
}
Also used : QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) QueryPassages(com.ibm.watson.developer_cloud.discovery.v1.model.QueryPassages) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 25 with QueryResponse

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

the class DiscoveryServiceIT method queryWithAggregationSummationIsSuccessful.

@Test
public void queryWithAggregationSummationIsSuccessful() throws InterruptedException {
    String collectionId = setupTestDocuments();
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    StringBuilder sb = new StringBuilder();
    sb.append(AggregationType.SUM);
    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 sum = (Calculation) queryResponse.getAggregations().get(0);
    assertEquals(AggregationType.SUM.getName(), sum.getType());
    assertEquals(new Double(45), sum.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)

Aggregations

QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)23 Test (org.junit.Test)21 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)20 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 QueryResponse (com.ibm.watson.discovery.v1.model.QueryResponse)7 LogQueryResponse (com.ibm.watson.discovery.v1.model.LogQueryResponse)6 JsonObject (com.google.gson.JsonObject)5 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)5 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 Calculation (com.ibm.watson.developer_cloud.discovery.v1.model.Calculation)4 FederatedQueryOptions (com.ibm.watson.discovery.v1.model.FederatedQueryOptions)4 QueryOptions (com.ibm.watson.discovery.v1.model.QueryOptions)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)3 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 GetMetricsQueryOptions (com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions)3