use of com.ibm.watson.discovery.v1.model.CreateCollectionOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method issueNumber659.
/* Issue 659: creating a collection does not use the configuration id */
@Test
public void issueNumber659() {
String uniqueConfigName = UUID.randomUUID().toString() + "-config";
Configuration testConfiguration = getTestConfiguration(DISCOVERY_TEST_CONFIG_FILE);
CreateConfigurationOptions configOptions = new CreateConfigurationOptions.Builder(environmentId).configuration(testConfiguration).name(uniqueConfigName).build();
Configuration configuration = discovery.createConfiguration(configOptions).execute();
configurationIds.add(configuration.getConfigurationId());
String uniqueCollectionName = UUID.randomUUID().toString() + "-collection";
CreateCollectionOptions collectionOptions = new CreateCollectionOptions.Builder(environmentId, uniqueCollectionName).configurationId(configuration.getConfigurationId()).build();
Collection collection = discovery.createCollection(collectionOptions).execute();
collectionIds.add(collection.getCollectionId());
assertEquals(collection.getConfigurationId(), configuration.getConfigurationId());
}
use of com.ibm.watson.discovery.v1.model.CreateCollectionOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method createCollectionWithMinimalParametersIsSuccessful.
@Test
public void createCollectionWithMinimalParametersIsSuccessful() {
String uniqueCollectionName = uniqueName + "-collection";
CreateCollectionOptions createOptions = new CreateCollectionOptions.Builder(environmentId, uniqueCollectionName).build();
Collection createResponse = createCollection(createOptions);
assertNotNull(createResponse.getCollectionId());
}
use of com.ibm.watson.discovery.v1.model.CreateCollectionOptions in project java-sdk by watson-developer-cloud.
the class Discovery method createCollection.
/**
* Create a collection.
*
* @param createCollectionOptions the {@link CreateCollectionOptions} containing the options for
* the call
* @return a {@link ServiceCall} with a result of type {@link Collection}
*/
public ServiceCall<Collection> createCollection(CreateCollectionOptions createCollectionOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(createCollectionOptions, "createCollectionOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", createCollectionOptions.environmentId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "createCollection");
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", createCollectionOptions.name());
if (createCollectionOptions.description() != null) {
contentJson.addProperty("description", createCollectionOptions.description());
}
if (createCollectionOptions.configurationId() != null) {
contentJson.addProperty("configuration_id", createCollectionOptions.configurationId());
}
if (createCollectionOptions.language() != null) {
contentJson.addProperty("language", createCollectionOptions.language());
}
builder.bodyJson(contentJson);
ResponseConverter<Collection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Collection>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v1.model.CreateCollectionOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method updateCollectionIsSuccessful.
@Test
public void updateCollectionIsSuccessful() {
String uniqueCollectionName = uniqueName + "-collection";
CreateCollectionOptions createOptions = new CreateCollectionOptions.Builder(environmentId, uniqueCollectionName).build();
Collection collection = createCollection(createOptions);
assertNotNull(collection.getCollectionId());
Configuration testConfig = createTestConfig();
String updatedCollectionName = UUID.randomUUID().toString() + "-collection";
String updatedCollectionDescription = "Description for " + updatedCollectionName;
UpdateCollectionOptions.Builder updateBuilder = new UpdateCollectionOptions.Builder(environmentId, collection.getCollectionId());
updateBuilder.name(updatedCollectionName);
updateBuilder.description(updatedCollectionDescription);
updateBuilder.configurationId(testConfig.getConfigurationId());
Collection updatedCollection = discovery.updateCollection(updateBuilder.build()).execute();
assertEquals(updatedCollectionName, updatedCollection.getName());
assertEquals(updatedCollectionDescription, updatedCollection.getDescription());
assertEquals(testConfig.getConfigurationId(), updatedCollection.getConfigurationId());
}
use of com.ibm.watson.discovery.v1.model.CreateCollectionOptions 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");
}
Aggregations