2017-08-07 72 views
1

我正嘗試通過Java中的IBM Watson API將文檔添加到IBM Bluemix Discovery實例的集合中。對於給定的文件,我做如下:IBM Watson:將文檔添加到集合時出現未知API錯誤

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初始化行。

謝謝。

回答

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

謝謝。它解決了這個問題。但是,現在我會提出一個問題。如果我想根據其路徑搜索特定的文檔,我該怎麼做?我需要文檔ID,看起來後者在CreateDocumentRequest的響應中只給出一次。我是否必須將這些ID映射到文件中的相應路徑?同樣的問題適用於刪除文件。 –

+0

我更新了它! – TonyY

相關問題