Search in sources :

Example 16 with DocumentAccepted

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);
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody)

Example 17 with DocumentAccepted

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());
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 18 with DocumentAccepted

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();
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) DeleteDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteDocumentOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 19 with DocumentAccepted

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 20 with DocumentAccepted

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) UpdateDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 InputStream (java.io.InputStream)20 Test (org.junit.Test)16 JsonObject (com.google.gson.JsonObject)14 FileInputStream (java.io.FileInputStream)14 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)14 JsonPrimitive (com.google.gson.JsonPrimitive)13 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)11 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)10 DocumentAccepted (com.ibm.watson.discovery.v1.model.DocumentAccepted)10 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)9 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 AddDocumentOptions (com.ibm.watson.discovery.v1.model.AddDocumentOptions)6 DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)6 MultipartBody (okhttp3.MultipartBody)6 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)5 UpdateDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions)5 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)4