Search in sources :

Example 36 with Configuration

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

the class Discovery method updateConfiguration.

/**
 * Update a configuration.
 *
 * Replaces an existing configuration. * Completely replaces the original configuration. * The `configuration_id`,
 * `updated`, and `created` fields are accepted in the request, but they are ignored, and an error is not generated.
 * It is also acceptable for users to submit an updated configuration with none of the three properties. * Documents
 * are processed with a snapshot of the configuration as it was at the time the document was submitted to be ingested.
 * This means that already submitted documents will not see any updates made to the configuration.
 *
 * @param updateConfigurationOptions the {@link UpdateConfigurationOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Configuration}
 */
public ServiceCall<Configuration> updateConfiguration(UpdateConfigurationOptions updateConfigurationOptions) {
    Validator.notNull(updateConfigurationOptions, "updateConfigurationOptions cannot be null");
    String[] pathSegments = { "v1/environments", "configurations" };
    String[] pathParameters = { updateConfigurationOptions.environmentId(), updateConfigurationOptions.configurationId() };
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (updateConfigurationOptions.name() != null) {
        contentJson.addProperty("name", updateConfigurationOptions.name());
    }
    if (updateConfigurationOptions.description() != null) {
        contentJson.addProperty("description", updateConfigurationOptions.description());
    }
    if (updateConfigurationOptions.conversions() != null) {
        contentJson.add("conversions", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.conversions()));
    }
    if (updateConfigurationOptions.enrichments() != null) {
        contentJson.add("enrichments", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.enrichments()));
    }
    if (updateConfigurationOptions.normalizations() != null) {
        contentJson.add("normalizations", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.normalizations()));
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Configuration.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Configuration(com.ibm.watson.developer_cloud.discovery.v1.model.Configuration) JsonObject(com.google.gson.JsonObject)

Example 37 with Configuration

use of com.ibm.watson.discovery.v1.model.Configuration 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 38 with Configuration

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

the class DiscoveryServiceTest method updateConfigurationIsSuccessful.

/**
 * Update configuration is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void updateConfigurationIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(updateConfResp));
    UpdateConfigurationOptions.Builder updateBuilder = new UpdateConfigurationOptions.Builder();
    updateBuilder.configurationId(configurationId);
    updateBuilder.environmentId(environmentId);
    Configuration newConf = new Configuration.Builder().name("newName").build();
    updateBuilder.configuration(newConf);
    Configuration response = discoveryService.updateConfiguration(updateBuilder.build()).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(CONF2_PATH, request.getPath());
    assertEquals(PUT, request.getMethod());
    assertEquals(updateConfResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Configuration(com.ibm.watson.discovery.v1.model.Configuration) UpdateConfigurationOptions(com.ibm.watson.discovery.v1.model.UpdateConfigurationOptions) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 39 with Configuration

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

the class DiscoveryServiceTest method getConfigurationIsSuccessful.

/**
 * Gets the configuration is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void getConfigurationIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(getConfResp));
    GetConfigurationOptions getRequest = new GetConfigurationOptions.Builder(environmentId, configurationId).build();
    Configuration response = discoveryService.getConfiguration(getRequest).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(CONF2_PATH, request.getPath());
    assertEquals(GET, request.getMethod());
    assertEquals(getConfResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) GetConfigurationOptions(com.ibm.watson.discovery.v1.model.GetConfigurationOptions) Configuration(com.ibm.watson.discovery.v1.model.Configuration) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 40 with Configuration

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

the class Discovery method updateConfiguration.

/**
 * Update a configuration.
 *
 * <p>Replaces an existing configuration. * Completely replaces the original configuration. * The
 * **configuration_id**, **updated**, and **created** fields are accepted in the request, but they
 * are ignored, and an error is not generated. It is also acceptable for users to submit an
 * updated configuration with none of the three properties. * Documents are processed with a
 * snapshot of the configuration as it was at the time the document was submitted to be ingested.
 * This means that already submitted documents will not see any updates made to the configuration.
 *
 * @param updateConfigurationOptions the {@link UpdateConfigurationOptions} containing the options
 *     for the call
 * @return a {@link ServiceCall} with a result of type {@link Configuration}
 */
public ServiceCall<Configuration> updateConfiguration(UpdateConfigurationOptions updateConfigurationOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateConfigurationOptions, "updateConfigurationOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", updateConfigurationOptions.environmentId());
    pathParamsMap.put("configuration_id", updateConfigurationOptions.configurationId());
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/configurations/{configuration_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "updateConfiguration");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("name", updateConfigurationOptions.name());
    if (updateConfigurationOptions.description() != null) {
        contentJson.addProperty("description", updateConfigurationOptions.description());
    }
    if (updateConfigurationOptions.conversions() != null) {
        contentJson.add("conversions", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.conversions()));
    }
    if (updateConfigurationOptions.enrichments() != null) {
        contentJson.add("enrichments", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.enrichments()));
    }
    if (updateConfigurationOptions.normalizations() != null) {
        contentJson.add("normalizations", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.normalizations()));
    }
    if (updateConfigurationOptions.source() != null) {
        contentJson.add("source", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.source()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Configuration> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Configuration>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Configuration(com.ibm.watson.discovery.v1.model.Configuration) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject)

Aggregations

Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)27 Test (org.junit.Test)21 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)18 Configuration (com.ibm.watson.discovery.v1.model.Configuration)10 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)8 CreateCollectionOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions)8 JsonObject (com.google.gson.JsonObject)7 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)7 CreateConfigurationOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateConfigurationOptions)7 HashMap (java.util.HashMap)7 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 GetConfigurationOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetConfigurationOptions)4 Conversions (com.ibm.watson.discovery.v1.model.Conversions)3 CreateConfigurationOptions (com.ibm.watson.discovery.v1.model.CreateConfigurationOptions)3 Enrichment (com.ibm.watson.discovery.v1.model.Enrichment)3 NormalizationOperation (com.ibm.watson.discovery.v1.model.NormalizationOperation)3 Source (com.ibm.watson.discovery.v1.model.Source)3