Search in sources :

Example 26 with QueryResponse

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

the class DiscoveryServiceIT method exampleIsSuccessful.

@Test
public void exampleIsSuccessful() {
    // Discovery discovery = new Discovery("2016-12-15");
    // discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
    // discovery.setUsernameAndPassword("<username>", "<password");
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(0L).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.getConfigurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection" + UUID.randomUUID();
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    createDocumentBuilder.filename("test_file");
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.developer_cloud.discovery.v1.model.Configuration) QueryOptions(com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions) GetEnvironmentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetEnvironmentOptions) GetCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetCollectionOptions) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) ListConfigurationsResponse(com.ibm.watson.developer_cloud.discovery.v1.model.ListConfigurationsResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ListConfigurationsOptions(com.ibm.watson.developer_cloud.discovery.v1.model.ListConfigurationsOptions) GetDocumentStatusOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions) DeleteCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteCollectionOptions) ListEnvironmentsResponse(com.ibm.watson.developer_cloud.discovery.v1.model.ListEnvironmentsResponse) DocumentStatus(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse) CreateEnvironmentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateEnvironmentOptions) Environment(com.ibm.watson.developer_cloud.discovery.v1.model.Environment) CreateCollectionOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) ListEnvironmentsOptions(com.ibm.watson.developer_cloud.discovery.v1.model.ListEnvironmentsOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 27 with QueryResponse

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

the class Discovery method federatedQuery.

/**
 * Query documents in multiple collections.
 *
 * See the [Discovery service documentation](https://console.bluemix.net/docs/services/discovery/using.html) for more
 * details.
 *
 * @param federatedQueryOptions the {@link FederatedQueryOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link QueryResponse}
 */
public ServiceCall<QueryResponse> federatedQuery(FederatedQueryOptions federatedQueryOptions) {
    Validator.notNull(federatedQueryOptions, "federatedQueryOptions cannot be null");
    String[] pathSegments = { "v1/environments", "query" };
    String[] pathParameters = { federatedQueryOptions.environmentId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    builder.query("collection_ids", RequestUtils.join(federatedQueryOptions.collectionIds(), ","));
    if (federatedQueryOptions.filter() != null) {
        builder.query("filter", federatedQueryOptions.filter());
    }
    if (federatedQueryOptions.query() != null) {
        builder.query("query", federatedQueryOptions.query());
    }
    if (federatedQueryOptions.naturalLanguageQuery() != null) {
        builder.query("natural_language_query", federatedQueryOptions.naturalLanguageQuery());
    }
    if (federatedQueryOptions.aggregation() != null) {
        builder.query("aggregation", federatedQueryOptions.aggregation());
    }
    if (federatedQueryOptions.count() != null) {
        builder.query("count", String.valueOf(federatedQueryOptions.count()));
    }
    if (federatedQueryOptions.returnFields() != null) {
        builder.query("return_fields", RequestUtils.join(federatedQueryOptions.returnFields(), ","));
    }
    if (federatedQueryOptions.offset() != null) {
        builder.query("offset", String.valueOf(federatedQueryOptions.offset()));
    }
    if (federatedQueryOptions.sort() != null) {
        builder.query("sort", RequestUtils.join(federatedQueryOptions.sort(), ","));
    }
    if (federatedQueryOptions.highlight() != null) {
        builder.query("highlight", String.valueOf(federatedQueryOptions.highlight()));
    }
    if (federatedQueryOptions.deduplicate() != null) {
        builder.query("deduplicate", String.valueOf(federatedQueryOptions.deduplicate()));
    }
    if (federatedQueryOptions.deduplicateField() != null) {
        builder.query("deduplicate.field", federatedQueryOptions.deduplicateField());
    }
    if (federatedQueryOptions.similar() != null) {
        builder.query("similar", String.valueOf(federatedQueryOptions.similar()));
    }
    if (federatedQueryOptions.similarDocumentIds() != null) {
        builder.query("similar.document_ids", RequestUtils.join(federatedQueryOptions.similarDocumentIds(), ","));
    }
    if (federatedQueryOptions.similarFields() != null) {
        builder.query("similar.fields", RequestUtils.join(federatedQueryOptions.similarFields(), ","));
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(QueryResponse.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)

Example 28 with QueryResponse

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

the class DiscoveryTest method testQueryWOptions.

@Test
public void testQueryWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"matching_results\": 15, \"results\": [{\"document_id\": \"documentId\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"result_metadata\": {\"document_retrieval_source\": \"search\", \"collection_id\": \"collectionId\", \"confidence\": 10}, \"document_passages\": [{\"passage_text\": \"passageText\", \"start_offset\": 11, \"end_offset\": 9, \"field\": \"field\", \"confidence\": 0, \"answers\": [{\"answer_text\": \"answerText\", \"start_offset\": 11, \"end_offset\": 9, \"confidence\": 0}]}]}], \"aggregations\": [{\"type\": \"filter\", \"match\": \"match\", \"matching_results\": 15}], \"retrieval_details\": {\"document_retrieval_strategy\": \"untrained\"}, \"suggested_query\": \"suggestedQuery\", \"suggested_refinements\": [{\"text\": \"text\"}], \"table_results\": [{\"table_id\": \"tableId\", \"source_document_id\": \"sourceDocumentId\", \"collection_id\": \"collectionId\", \"table_html\": \"tableHtml\", \"table_html_offset\": 15, \"table\": {\"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"section_title\": {\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}, \"title\": {\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}, \"table_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"mapKey\": \"anyValue\"}, \"text\": \"text\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"row_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"column_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"mapKey\": \"anyValue\"}, \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"key_value_pairs\": [{\"key\": {\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\"}, \"value\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\"}]}], \"body_cells\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14, \"row_header_ids\": [{\"id\": \"id\"}], \"row_header_texts\": [{\"text\": \"text\"}], \"row_header_texts_normalized\": [{\"text_normalized\": \"textNormalized\"}], \"column_header_ids\": [{\"id\": \"id\"}], \"column_header_texts\": [{\"text\": \"text\"}], \"column_header_texts_normalized\": [{\"text_normalized\": \"textNormalized\"}], \"attributes\": [{\"type\": \"type\", \"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}]}], \"contexts\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}]}}], \"passages\": [{\"passage_text\": \"passageText\", \"passage_score\": 12, \"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"start_offset\": 11, \"end_offset\": 9, \"field\": \"field\", \"confidence\": 0, \"answers\": [{\"answer_text\": \"answerText\", \"start_offset\": 11, \"end_offset\": 9, \"confidence\": 0}]}]}";
    String queryPath = "/v2/projects/testString/query";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the QueryLargeTableResults model
    QueryLargeTableResults queryLargeTableResultsModel = new QueryLargeTableResults.Builder().enabled(true).count(Long.valueOf("26")).build();
    // Construct an instance of the QueryLargeSuggestedRefinements model
    QueryLargeSuggestedRefinements queryLargeSuggestedRefinementsModel = new QueryLargeSuggestedRefinements.Builder().enabled(true).count(Long.valueOf("1")).build();
    // Construct an instance of the QueryLargePassages model
    QueryLargePassages queryLargePassagesModel = new QueryLargePassages.Builder().enabled(true).perDocument(true).maxPerDocument(Long.valueOf("26")).fields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).count(Long.valueOf("400")).characters(Long.valueOf("50")).findAnswers(false).maxAnswersPerPassage(Long.valueOf("26")).build();
    // Construct an instance of the QueryOptions model
    QueryOptions queryOptionsModel = new QueryOptions.Builder().projectId("testString").collectionIds(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).filter("testString").query("testString").naturalLanguageQuery("testString").aggregation("testString").count(Long.valueOf("26")).xReturn(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).offset(Long.valueOf("26")).sort("testString").highlight(true).spellingSuggestions(true).tableResults(queryLargeTableResultsModel).suggestedRefinements(queryLargeSuggestedRefinementsModel).passages(queryLargePassagesModel).build();
    // Invoke operation with valid options model (positive test)
    Response<QueryResponse> response = discoveryService.query(queryOptionsModel).execute();
    assertNotNull(response);
    QueryResponse responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, queryPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) QueryLargeSuggestedRefinements(com.ibm.watson.discovery.v2.model.QueryLargeSuggestedRefinements) QueryLargePassages(com.ibm.watson.discovery.v2.model.QueryLargePassages) DeleteTrainingQueryOptions(com.ibm.watson.discovery.v2.model.DeleteTrainingQueryOptions) GetTrainingQueryOptions(com.ibm.watson.discovery.v2.model.GetTrainingQueryOptions) CreateTrainingQueryOptions(com.ibm.watson.discovery.v2.model.CreateTrainingQueryOptions) UpdateTrainingQueryOptions(com.ibm.watson.discovery.v2.model.UpdateTrainingQueryOptions) QueryOptions(com.ibm.watson.discovery.v2.model.QueryOptions) QueryResponse(com.ibm.watson.discovery.v2.model.QueryResponse) QueryLargeTableResults(com.ibm.watson.discovery.v2.model.QueryLargeTableResults) Test(org.testng.annotations.Test)

Example 29 with QueryResponse

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

the class DiscoveryQueryExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    Discovery discovery = new Discovery("2019-04-30", authenticator);
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute().getResult();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(String.valueOf(0L)).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute().getResult();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute().getResult();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute().getResult();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.name().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.configurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection";
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute().getResult();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute().getResult();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute().getResult();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute().getResult();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.discovery.v1.model.Configuration) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) QueryOptions(com.ibm.watson.discovery.v1.model.QueryOptions) GetEnvironmentOptions(com.ibm.watson.discovery.v1.model.GetEnvironmentOptions) GetCollectionOptions(com.ibm.watson.discovery.v1.model.GetCollectionOptions) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) ListConfigurationsResponse(com.ibm.watson.discovery.v1.model.ListConfigurationsResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ListConfigurationsOptions(com.ibm.watson.discovery.v1.model.ListConfigurationsOptions) GetDocumentStatusOptions(com.ibm.watson.discovery.v1.model.GetDocumentStatusOptions) DeleteCollectionOptions(com.ibm.watson.discovery.v1.model.DeleteCollectionOptions) ListEnvironmentsResponse(com.ibm.watson.discovery.v1.model.ListEnvironmentsResponse) DocumentStatus(com.ibm.watson.discovery.v1.model.DocumentStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) CreateEnvironmentOptions(com.ibm.watson.discovery.v1.model.CreateEnvironmentOptions) Environment(com.ibm.watson.discovery.v1.model.Environment) CreateCollectionOptions(com.ibm.watson.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.discovery.v1.model.Collection) ListEnvironmentsOptions(com.ibm.watson.discovery.v1.model.ListEnvironmentsOptions)

Example 30 with QueryResponse

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

the class DiscoveryServiceTest method queryWithAggregationTermIsSuccessful.

/**
 * Query with aggregation term is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void queryWithAggregationTermIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(queryResp));
    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);
    String aggregation = sb.toString();
    queryBuilder.aggregation(aggregation);
    QueryResponse response = discoveryService.query(queryBuilder.build()).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(Q1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(GsonSingleton.getGson().toJsonTree(queryResp), GsonSingleton.getGson().toJsonTree(response));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) LogQueryResponse(com.ibm.watson.discovery.v1.model.LogQueryResponse) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) FederatedQueryOptions(com.ibm.watson.discovery.v1.model.FederatedQueryOptions) GetMetricsQueryOptions(com.ibm.watson.discovery.v1.model.GetMetricsQueryOptions) QueryOptions(com.ibm.watson.discovery.v1.model.QueryOptions) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

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