Search in sources :

Example 36 with DocumentAccepted

use of com.ibm.watson.discovery.v1.model.DocumentAccepted 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.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