IBM Watson:将文档添加到集合

时间:2017-08-07 13:42:35

标签: java watson-discovery

我正在尝试通过Java中的IBM Watson API将文档添加到IBM Bluemix Discovery实例的集合中。对于给定的文件,我执行如下操作:

Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
discovery.setUsernameAndPassword("{username}", "{password}");
String environmentId = "{environment_id}";
String collectionId = "{collection_id}";

File f = new File("path/to/file");
String path = f.getPath();
String ext = FilenameUtils.getExtension(f.getName());

CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId).documentId(path).file(f);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
System.out.println(createResponse.toString());

然而,当我执行我的程序时,我收到错误Exception in thread "main" com.ibm.watson.developer_cloud.service.exception.NotFoundException: Unknown API: POST [...],该错误来自createResponse初始化的行。

谢谢。

1 个答案:

答案 0 :(得分:4)

你有错误的documentId。

正确的documentId就像“49a82bd4-xxxx-xxxx-xxxx-900b7378ff35”,但你把路径作为你的参数。

如果要添加文档,默认情况下不需要为documentId指定值。

Discovery discovery = new Discovery("2017-08-01");
discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");                             
discovery.setUsernameAndPassword("feeb4a95-xxxx-xxxx-xxxx-4d94c98b2b24", "KQc6G5nK6HrD");
String environmentId = "59d0fa32-xxxx-xxxx-xxxx-cf98a8cdc52e";
String collectionId = "49a82bd4-xxxx-xxxx-xxxx-900b7378ff35";
File f = new File("/Users/xxx/Desktop/myDoc.json");
Path path = Paths.get(f.getPath());
InputStream documentStream = new ByteArrayInputStream(Files.readAllBytes(path));

//Create
CreateDocumentRequest.Builder builder = new CreateDocumentRequest.Builder(environmentId, collectionId);
builder.inputStream(documentStream, HttpMediaType.APPLICATION_JSON);
CreateDocumentResponse createResponse = discovery.createDocument(builder.build()).execute();
//Retrieve  (You can get documentId here)
QueryRequest.Builder queryBuilder = new QueryRequest.Builder(environmentId, collectionId);
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
System.out.println(queryResponse.toString());
//Delete
String documentId = "d1059598-xxxx-xxxx-xxxx-b65a1f3334d8";
DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest.Builder(environmentId, collectionId, documentId).build();
DeleteDocumentResponse deleteResponse = discovery.deleteDocument(deleteRequest).execute();