Search in sources :

Example 31 with Collection

use of com.ibm.watson.developer_cloud.discovery.v1.model.Collection 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 response type of {@link Collection}
 */
public ServiceCall<Collection> createCollection(CreateCollectionOptions createCollectionOptions) {
    Validator.notNull(createCollectionOptions, "createCollectionOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections" };
    String[] pathParameters = { createCollectionOptions.environmentId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    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);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Collection.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection)

Example 32 with Collection

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

the class Discovery method addDocument.

/**
 * Add a document.
 *
 * Add a document to a collection with optional metadata. * The `version` query parameter is still required. * Returns
 * immediately after the system has accepted the document for processing. * The user must provide document content,
 * metadata, or both. If the request is missing both document content and metadata, it is rejected. * The user can set
 * the `Content-Type` parameter on the `file` part to indicate the media type of the document. If the `Content-Type`
 * parameter is missing or is one of the generic media types (for example, `application/octet-stream`), then the
 * service attempts to automatically detect the document's media type. * The following field names are reserved and
 * will be filtered out if present after normalization: `id`, `score`, `highlight`, and any field with the prefix of:
 * `_`, `+`, or `-` * Fields with empty name values after normalization are filtered out before indexing. * Fields
 * containing the following characters after normalization are filtered out before indexing: `#` and `,`.
 *
 * @param addDocumentOptions the {@link AddDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link DocumentAccepted}
 */
public ServiceCall<DocumentAccepted> addDocument(AddDocumentOptions addDocumentOptions) {
    Validator.notNull(addDocumentOptions, "addDocumentOptions cannot be null");
    Validator.isTrue((addDocumentOptions.file() != null) || (addDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    String[] pathSegments = { "v1/environments", "collections", "documents" };
    String[] pathParameters = { addDocumentOptions.environmentId(), addDocumentOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (addDocumentOptions.file() != null) {
        RequestBody fileBody = RequestUtils.inputStreamBody(addDocumentOptions.file(), addDocumentOptions.fileContentType());
        multipartBuilder.addFormDataPart("file", addDocumentOptions.filename(), fileBody);
    }
    if (addDocumentOptions.metadata() != null) {
        multipartBuilder.addFormDataPart("metadata", addDocumentOptions.metadata());
    }
    builder.body(multipartBuilder.build());
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(DocumentAccepted.class));
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) RequestBody(okhttp3.RequestBody)

Example 33 with Collection

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

the class Discovery method createExpansions.

/**
 * Set the expansion list.
 *
 * Create or replace the Expansion list for this collection. The maximum number of expanded terms per collection is
 * `500`. The current expansion list is replaced with the uploaded content.
 *
 * @param createExpansionsOptions the {@link CreateExpansionsOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Expansions}
 */
public ServiceCall<Expansions> createExpansions(CreateExpansionsOptions createExpansionsOptions) {
    Validator.notNull(createExpansionsOptions, "createExpansionsOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections", "expansions" };
    String[] pathParameters = { createExpansionsOptions.environmentId(), createExpansionsOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (createExpansionsOptions.expansions() != null) {
        contentJson.add("expansions", GsonSingleton.getGson().toJsonTree(createExpansionsOptions.expansions()));
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Expansions.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) Expansions(com.ibm.watson.developer_cloud.discovery.v1.model.Expansions)

Example 34 with Collection

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

the class Discovery method addTrainingData.

/**
 * Adds a query to the training data for this collection. The query can contain a filter and natural language query.
 *
 * @param addTrainingDataOptions the {@link AddTrainingDataOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link TrainingQuery}
 */
public ServiceCall<TrainingQuery> addTrainingData(AddTrainingDataOptions addTrainingDataOptions) {
    Validator.notNull(addTrainingDataOptions, "addTrainingDataOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections", "training_data" };
    String[] pathParameters = { addTrainingDataOptions.environmentId(), addTrainingDataOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (addTrainingDataOptions.naturalLanguageQuery() != null) {
        contentJson.addProperty("natural_language_query", addTrainingDataOptions.naturalLanguageQuery());
    }
    if (addTrainingDataOptions.filter() != null) {
        contentJson.addProperty("filter", addTrainingDataOptions.filter());
    }
    if (addTrainingDataOptions.examples() != null) {
        contentJson.add("examples", GsonSingleton.getGson().toJsonTree(addTrainingDataOptions.examples()));
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TrainingQuery.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery)

Aggregations

Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)26 Test (org.junit.Test)21 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)19 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)11 CreateCollectionOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateCollectionOptions)10 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)9 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)8 JsonObject (com.google.gson.JsonObject)7 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)5 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)5 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)5 DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)4 GetDocumentStatusOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions)4 Ignore (org.junit.Ignore)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 GetCollectionOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetCollectionOptions)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2