簡體   English   中英

我需要幫助來確定watson-developer-cloud Node.js函數的正確語法,以將文檔插入Watson的Discovery服務

[英]I need help figuring out the correct syntax for the watson-developer-cloud Node.js function to insert a document into Watson's Discovery service

我正在嘗試使用Node.js watson-developer-cloud JDK將JSON文檔插入Discovery集合中。 這是相關代碼:

var DiscoveryV1=require('watson-developer-cloud/discovery/v1');
var discovery=new DiscoveryV1(credentials);

let doc=aSmallValidJsonObject;
let parms={
  environment_id: envID,
  collection_id: collID,
  configuration_id: confID,
  file: {
        value: new Buffer(JSON.stringify(doc)),
        options:{
          contentType:'application/json',
          "Content-Type":'application/json' //just to be sure
        }
     }
  };
discovery.addDocument(parms,
  function(err,results)
    {
    if (err) {...

此調用返回的錯誤是

"Error: Request must specify either a "metadata" or "file" part"

我也嘗試過通過以下方式制作Parms

let parms={
  environment_id: envID,
  collection_id: collID,
  configuration_id: confID,
  metadata:{"Content-Type":"application/json"},
  file:new Buffer(JSON.stringify(doc))
  };

我在這種情況下得到的錯誤是

[TypeError: source.on is not a function]

(我將此錯誤追溯到庫delay_stream.js中的第33行)

如果在上述參數中將元數據字段設置為字符串(即,將值括在單引號中),則會出現此錯誤:

Error: 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

有人可以告訴我此功能的正確語法是什么嗎?

使用watson-developer-cloud SDK和在不使用SDK的情況下直接發布到API時,我遇到了同樣的問題。 使它起作用的唯一方法是忽略文件,並將我的文檔內容作為元數據的一部分直接發送到API(例如,使用請求包)。

var options = {
    uri:'https://gateway.watsonplatform.net/discovery/api/v1/environments/'+process.env.DISCOVERY_ENVIRONMENT_ID+'/collections/'+process.env.DISCOVERY_COLLECTION_ID+'/documents?version=2016-12-01&configuration_id='+process.env.DISCOVERY_CONFIGURATION_ID,
    method: 'POST',
    formData: {
        metadata: {
            doc: JSON.stringify(doc)
        }
    },
    auth: {
       user: process.env.DISCOVERY_USERNAME,
       pass: process.env.DISCOVERY_PASSWORD
    },
};

request(options, function(err, httpResponse, body){
    if(err){
      console.log(err);
    }
    console.dir(body);
});

然后,您需要配置您的集合以使用元數據中的文檔進行任何擴充,並記住針對元數據進行查詢。

如果要在Computer HD中發送一個文檔,則要求FileSystem( fs )庫正確發送文檔。 該錯誤似乎無法識別您的選項中的標題。 嘗試官方示例或我的示例:

官方API參考顯示了addDocument的一個示例,例如:

var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
var fs = require('fs');

var discovery = new DiscoveryV1({
  username: '{username}',
  password: '{password}',
  version_date: '2017-06-25'
});

var file = fs.readFileSync('{/path/to/file}');

discovery.addDocument(('{environment_id}', '{collection_id}', file),
function(error, data) {
  console.log(JSON.stringify(data, null, 2));
});

以您的示例為例:

discovery.addDocument({
    environment_id: yourEnvId,
    collection_id: yourCollId,
    metadata:'{"Content-Type":"application/json"}',
    file: Buffer.from(doc)
}, function(err, data) {
    if (err) {
        return next(err);
    } else {
        return res.json(data)
      }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM