use of com.ibm.watson.discovery.v2.model.DocumentAccepted in project java-sdk by watson-developer-cloud.
the class Discovery method updateDocument.
/**
* Update a document.
*
* <p>Replace an existing document or add a document with a specified **document_id**. Starts
* ingesting a document with optional metadata.
*
* <p>If the document is uploaded to a collection that shares its data with another collection,
* the **X-Watson-Discovery-Force** header must be set to `true`.
*
* <p>**Note:** When uploading a new document with this method it automatically replaces any
* document stored with the same **document_id** if it exists.
*
* <p>**Note:** This operation only works on collections created to accept direct file uploads. It
* cannot be used to modify a collection that connects to an external source such as Microsoft
* SharePoint.
*
* <p>**Note:** If an uploaded document is segmented, all segments are overwritten, even if the
* updated version of the document has fewer segments.
*
* @param updateDocumentOptions the {@link UpdateDocumentOptions} containing the options for the
* call
* @return a {@link ServiceCall} with a result of type {@link DocumentAccepted}
*/
public ServiceCall<DocumentAccepted> updateDocument(UpdateDocumentOptions updateDocumentOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(updateDocumentOptions, "updateDocumentOptions cannot be null");
com.ibm.cloud.sdk.core.util.Validator.isTrue((updateDocumentOptions.file() != null) || (updateDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("project_id", updateDocumentOptions.projectId());
pathParamsMap.put("collection_id", updateDocumentOptions.collectionId());
pathParamsMap.put("document_id", updateDocumentOptions.documentId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections/{collection_id}/documents/{document_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "updateDocument");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (updateDocumentOptions.xWatsonDiscoveryForce() != null) {
builder.header("X-Watson-Discovery-Force", updateDocumentOptions.xWatsonDiscoveryForce());
}
builder.query("version", String.valueOf(this.version));
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (updateDocumentOptions.file() != null) {
okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(updateDocumentOptions.file(), updateDocumentOptions.fileContentType());
multipartBuilder.addFormDataPart("file", updateDocumentOptions.filename(), fileBody);
}
if (updateDocumentOptions.metadata() != null) {
multipartBuilder.addFormDataPart("metadata", updateDocumentOptions.metadata());
}
builder.body(multipartBuilder.build());
ResponseConverter<DocumentAccepted> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentAccepted>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v2.model.DocumentAccepted in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method addDocumentWithConfigurationIsSuccessful.
@Test
public void addDocumentWithConfigurationIsSuccessful() {
Collection collection = createTestCollection();
uniqueName = UUID.randomUUID().toString();
String myDocumentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder();
builder.environmentId(environmentId);
builder.collectionId(collection.getCollectionId());
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
builder.filename("test_file");
DocumentAccepted createResponse = discovery.addDocument(builder.build()).execute();
assertFalse(createResponse.getDocumentId().isEmpty());
assertNull(createResponse.getNotices());
}
use of com.ibm.watson.discovery.v2.model.DocumentAccepted in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method deleteDocumentIsSuccessful.
@Test
public void deleteDocumentIsSuccessful() {
Collection collection = createTestCollection();
String collectionId = collection.getCollectionId();
DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
DeleteDocumentOptions deleteOptions = new DeleteDocumentOptions.Builder(environmentId, collectionId, documentAccepted.getDocumentId()).build();
discovery.deleteDocument(deleteOptions).execute();
}
use of com.ibm.watson.discovery.v2.model.DocumentAccepted in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method addDocumentFromInputStreamWithMediaTypeIsSuccessful.
@Test
public void addDocumentFromInputStreamWithMediaTypeIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(createDocResp));
String myDocumentJson = "{\"field\":\"value\"}";
JsonObject myMetadata = new JsonObject();
myMetadata.add("foo", new JsonPrimitive("bar"));
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
builder.filename("test_file");
builder.metadata(myMetadata.toString());
DocumentAccepted response = discoveryService.addDocument(builder.build()).execute();
RecordedRequest request = server.takeRequest();
assertEquals(DOCS1_PATH, request.getPath());
assertEquals(POST, request.getMethod());
assertEquals(createDocResp, response);
}
use of com.ibm.watson.discovery.v2.model.DocumentAccepted in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method updateDocumentIsSuccessful.
// Deleted tests for (create)addDocument with file parameter as this is deprecated
@Test
public void updateDocumentIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(updateDocResp));
UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentId);
String myDocumentJson = "{\"field\":\"value2\"}";
JsonObject myMetadata = new JsonObject();
myMetadata.add("foo", new JsonPrimitive("bar"));
InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
updateBuilder.filename("test_file");
updateBuilder.metadata(myMetadata.toString());
DocumentAccepted response = discoveryService.updateDocument(updateBuilder.build()).execute();
RecordedRequest request = server.takeRequest();
assertEquals(DOCS2_PATH, request.getPath());
assertEquals(POST, request.getMethod());
assertEquals(updateDocResp, response);
}
Aggregations