Search in sources :

Example 26 with DocumentAccepted

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

the class Discovery method updateDocument.

/**
 * Update a document.
 *
 * Replace an existing document. Starts ingesting a document with optional metadata.
 *
 * @param updateDocumentOptions the {@link UpdateDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link DocumentAccepted}
 */
public ServiceCall<DocumentAccepted> updateDocument(UpdateDocumentOptions updateDocumentOptions) {
    Validator.notNull(updateDocumentOptions, "updateDocumentOptions cannot be null");
    Validator.isTrue((updateDocumentOptions.file() != null) || (updateDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    String[] pathSegments = { "v1/environments", "collections", "documents" };
    String[] pathParameters = { updateDocumentOptions.environmentId(), updateDocumentOptions.collectionId(), updateDocumentOptions.documentId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (updateDocumentOptions.file() != null) {
        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());
    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)

Example 27 with DocumentAccepted

use of com.ibm.watson.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)

Example 28 with DocumentAccepted

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

the class DiscoveryTest method testAddDocumentWOptions.

@Test
public void testAddDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\"}";
    String addDocumentPath = "/v2/projects/testString/collections/testString/documents";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the AddDocumentOptions model
    AddDocumentOptions addDocumentOptionsModel = new AddDocumentOptions.Builder().projectId("testString").collectionId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").xWatsonDiscoveryForce(false).build();
    // Invoke operation with valid options model (positive test)
    Response<DocumentAccepted> response = discoveryService.addDocument(addDocumentOptionsModel).execute();
    assertNotNull(response);
    DocumentAccepted responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, addDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) Test(org.testng.annotations.Test)

Example 29 with DocumentAccepted

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

the class DiscoveryQueryExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    Discovery discovery = new Discovery("2019-04-30", authenticator);
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute().getResult();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(String.valueOf(0L)).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute().getResult();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute().getResult();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute().getResult();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.name().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.configurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection";
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute().getResult();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute().getResult();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute().getResult();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute().getResult();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.discovery.v1.model.Configuration) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) QueryOptions(com.ibm.watson.discovery.v1.model.QueryOptions) GetEnvironmentOptions(com.ibm.watson.discovery.v1.model.GetEnvironmentOptions) GetCollectionOptions(com.ibm.watson.discovery.v1.model.GetCollectionOptions) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) ListConfigurationsResponse(com.ibm.watson.discovery.v1.model.ListConfigurationsResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ListConfigurationsOptions(com.ibm.watson.discovery.v1.model.ListConfigurationsOptions) GetDocumentStatusOptions(com.ibm.watson.discovery.v1.model.GetDocumentStatusOptions) DeleteCollectionOptions(com.ibm.watson.discovery.v1.model.DeleteCollectionOptions) ListEnvironmentsResponse(com.ibm.watson.discovery.v1.model.ListEnvironmentsResponse) DocumentStatus(com.ibm.watson.discovery.v1.model.DocumentStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) CreateEnvironmentOptions(com.ibm.watson.discovery.v1.model.CreateEnvironmentOptions) Environment(com.ibm.watson.discovery.v1.model.Environment) CreateCollectionOptions(com.ibm.watson.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.discovery.v1.model.Collection) ListEnvironmentsOptions(com.ibm.watson.discovery.v1.model.ListEnvironmentsOptions)

Example 30 with DocumentAccepted

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

the class DiscoveryServiceTest method addDocumentFromInputStreamIsSuccessful.

/**
 * Adds the document from input stream is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void addDocumentFromInputStreamIsSuccessful() 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);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute().getResult();
    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.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

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