我正在尝试索引 JSON 文档,但它根本不起作用;到目前为止,我已经尝试了https://developer.ibm.com/answers/questions/361808/adding-a-json-document-to-a-discovery-collection-u/中发布的解决方案,但它根本没有工作;
如果我尝试:
discovery.addDocument({
environment_id: config.watson.environment_id,
collection_id: config.watson.collection_id,
file: JSON.stringify({
"ocorrencia_id": 9001
})
}, (error, data) => {
if (error) {
console.error(error);
return;
}
console.log(data);
});
它返回给我这个错误:
The Media Type [text/plain] of the input document is not supported. Auto correction was attempted, but the auto detected media type [text/plain] is also not supported. Supported Media Types are: application/json, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, text/html, application/xhtml+xml .
另一方面,如果我尝试:
discovery.addDocument({
environment_id: config.watson.environment_id,
collection_id: config.watson.collection_id,
file: JSON.parse(JSON.stringify({
"ocorrencia_id": 9001
}))
}, (error, data) => {
if (error) {
console.error(error);
return;
}
console.log(data);
});
我收到此错误:
TypeError: source.on is not a function
at Function.DelayedStream.create (C:\Temp\teste-watson\watson-orchestrator\node_modules\delayed-stream\lib\delayed_stream.js:33:10)
at FormData.CombinedStream.append (C:\Temp\teste-watson\watson-orchestrator\node_modules\combined-stream\lib\combined_stream.js:43:37)
at FormData.append (C:\Temp\teste-watson\watson-orchestrator\node_modules\form-data\lib\form_data.js:68:3)
at appendFormValue (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:324:21)
at Request.init (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:337:11)
at new Request (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:130:8)
at request (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\index.js:54:10)
at createRequest (C:\Temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\lib\requestwrapper.js:177:10)
at DiscoveryV1.addDocument (C:\Temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\discovery\v1.js:516:10)
at client.query.then.res (C:\Temp\teste-watson\watson-orchestrator\populate\populate.js:36:13)
at process._tickCallback (internal/process/next_tick.js:109:7)
同样,通过保存到临时文件,然后使用它:
const tempy = require('tempy');
const f = tempy.file({extension: 'json'});
fs.writeFileSync(f, JSON.stringify({
"ocorrencia_id": 9001
}));
discovery.addDocument({
environment_id: config.watson.environment_id,
collection_id: config.watson.collection_id,
file: fs.readFileSync(f)
}, (error, data) => {
if (error) {
console.error(error);
return;
}
console.log(data);
});
然后会发生这种情况:
The Media Type [application/octet-stream] of the input document is not supported. Auto correction was attempted, but the auto detected media type [text/plain] is also not supported. Supported Media Types are: application/json, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, text/html, application/xhtml+xml .
考虑到其他帖子建议使用 JSON.parse(),似乎 API 接受了一个 JS 对象,但没有一个示例,而且到目前为止我尝试过的任何方法似乎都不起作用。似乎是一个错误?
更新:通过保存到临时文件中,然后使用createDataStream()
, 而不是readFileSync()
,它可以工作,但是必须通过磁盘获取已经在内存中的信息仍然很麻烦。
我也尝试从 Readable 创建一个内存流,但这也失败了:
var Readable = require('stream').Readable;
var s = new Readable();
s._read = function noop() {}; // redundant? see update below
s.push(JSON.stringify({
"ocorrencia_id": 9001
}));
s.push(null);
discovery.addDocument({
environment_id: config.watson.environment_id,
collection_id: config.watson.collection_id,
file: s
}, (error, data) => {
if (error) {
console.error(error);
return;
}
console.log(data);
});
这个失败了:
Error: Unexpected end of multipart data
at Request._callback (C:\Temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\lib\requestwrapper.js:88:15)
at Request.self.callback (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Gunzip.<anonymous> (C:\Temp\teste-watson\watson-orchestrator\node_modules\request\request.js:1091:12)
at Gunzip.g (events.js:292:16)
at emitNone (events.js:91:20)
at Gunzip.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9) code: 500, error: 'Unexpected end of multipart data'