0
votes

Test Java API for IBM Watson discovery service

Discovery discovery = new Discovery("2017-09-01"); 

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();

Should work without throwing any exception.

An exception is thrown on the last statment "discovery.addDocument"; error message is "filename cannot be null when file is not null"

1

1 Answers

0
votes

Since you are using documentJson parameter, are you sure that JSON metadata has fileName. If not define a string with variable name fileName. FYR Add Document to Discovery

Discovery discovery = new Discovery("2017-11-07");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api/");
discovery.setUsernameAndPassword("{username}", "{password}");
String environmentId = "{environment_id}";
String collectionId = "{collection_id}";
String documentId = "{document_id}";
String fileName = "<fileName>";

FYI - You cannot use documentStream and fileName parameter together