Search in sources :

Example 1 with AddDocumentOptions

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

the class DiscoveryServiceTest method addDocumentWithoutRequiredParametersFails.

@Test(expected = IllegalArgumentException.class)
public void addDocumentWithoutRequiredParametersFails() {
    AddDocumentOptions options = new AddDocumentOptions.Builder(environmentId, collectionId).build();
    DocumentAccepted response = discoveryService.addDocument(options).execute();
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with AddDocumentOptions

use of com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions 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)

Aggregations

DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 Test (org.junit.Test)1