Search in sources :

Example 21 with DocumentAccepted

use of com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted 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)20 Test (org.junit.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 InputStream (java.io.InputStream)14 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)11 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)10 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)9 JsonObject (com.google.gson.JsonObject)8 JsonPrimitive (com.google.gson.JsonPrimitive)8 FileInputStream (java.io.FileInputStream)8 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 UpdateDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)4 GetDocumentStatusOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions)4 WaitFor (com.ibm.watson.developer_cloud.util.WaitFor)4 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)3 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)3 Ignore (org.junit.Ignore)3 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)2