Search in sources :

Example 6 with AddDocumentOptions

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

the class Discovery method addDocument.

/**
 * Add a document.
 *
 * <p>Add a document to a collection with optional metadata.
 *
 * <p>Returns immediately after the system has accepted the document for processing.
 *
 * <p>* The user must provide document content, metadata, or both. If the request is missing both
 * document content and metadata, it is rejected.
 *
 * <p>* You 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.
 *
 * <p>* The following field names are reserved and are filtered out if present after
 * normalization: `id`, `score`, `highlight`, and any field with the prefix of: `_`, `+`, or `-`
 *
 * <p>* Fields with empty name values after normalization are filtered out before indexing.
 *
 * <p>* Fields that contain the following characters after normalization are filtered out before
 * indexing: `#` and `,`
 *
 * <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:** You can assign an ID to a document that you add by appending the ID to the
 * endpoint (`/v2/projects/{project_id}/collections/{collection_id}/documents/{document_id}`). If
 * a document already exists with the specified ID, it is replaced.
 *
 * <p>**Note:** This operation works with a file upload collection. It cannot be used to modify a
 * collection that crawls an external data source.
 *
 * @param addDocumentOptions the {@link AddDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentAccepted}
 */
public ServiceCall<DocumentAccepted> addDocument(AddDocumentOptions addDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(addDocumentOptions, "addDocumentOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.isTrue((addDocumentOptions.file() != null) || (addDocumentOptions.metadata() != null), "At least one of file or metadata must be supplied.");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("project_id", addDocumentOptions.projectId());
    pathParamsMap.put("collection_id", addDocumentOptions.collectionId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/projects/{project_id}/collections/{collection_id}/documents", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v2", "addDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (addDocumentOptions.xWatsonDiscoveryForce() != null) {
        builder.header("X-Watson-Discovery-Force", addDocumentOptions.xWatsonDiscoveryForce());
    }
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (addDocumentOptions.file() != null) {
        okhttp3.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());
    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 7 with AddDocumentOptions

use of com.ibm.watson.discovery.v2.model.AddDocumentOptions in project nlp4j by oyahiroki.

the class WD2DocumentImporter method importDocument.

@Override
public void importDocument(Document doc) throws IOException {
    String fileName;
    if (doc.getAttribute("filename") != null) {
        fileName = doc.getAttributeAsString("filename");
    } else {
        fileName = "hello" + System.currentTimeMillis() + ".json";
    }
    JsonObject jsonObj = DocumentUtil.toJsonObject(doc);
    jsonObj.remove("keywords");
    {
        IamAuthenticator authenticator = new IamAuthenticator(this.DISCOVERY_APIKEY);
        Discovery v2Discovery = new Discovery("2020-08-30", authenticator);
        v2Discovery.setServiceUrl(this.DISCOVERY_URL);
        AddDocumentOptions options = // 
        new AddDocumentOptions.Builder().projectId(// 
        this.projectId).collectionId(// 
        this.collectionId).file(// 
        new ByteArrayInputStream(jsonObj.toString().getBytes(StandardCharsets.UTF_8))).filename(// 
        fileName).fileContentType(// 
        "application/json").build();
        DocumentAccepted response = v2Discovery.addDocument(options).execute().getResult();
        System.err.println(response.getStatus());
        System.err.println(response);
    }
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Discovery(com.ibm.watson.discovery.v2.Discovery) JsonObject(com.google.gson.JsonObject)

Aggregations

DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)4 AddDocumentOptions (com.ibm.watson.discovery.v2.model.AddDocumentOptions)3 AddDocumentOptions (com.ibm.watson.discovery.v1.model.AddDocumentOptions)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.testng.annotations.Test)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1 BearerTokenAuthenticator (com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator)1 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)1 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)1 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)1 DocumentAccepted (com.ibm.watson.discovery.v1.model.DocumentAccepted)1 Discovery (com.ibm.watson.discovery.v2.Discovery)1 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)1 QueryOptions (com.ibm.watson.discovery.v2.model.QueryOptions)1 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)1