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));
}
Aggregations