Create new document in IBM Watson Discovery Service with Watson Developer Cloud Node SDK

936 views Asked by At

i try to create a new document for the IBM Watson Discovery Service and I use the watson-developer-cloud/node-sdk

The content in the document should come from a string and not from a file. I tried like this but with no luck

 discovery.addDocument({
    environment_id: MYENVID,
    collection_id: MYCOLID,
    metadata:'{"Content-Type":"application/json"}',
    file:Buffer.from("HERE IS MY TEXT", 'utf8')
}, function(err, data) {
    if (err) {
        return next(err);
    } else {
        return res.json(data)
    }
});

this creates a document but with no content. This is how the result looks like

{
    "id": "MYID",
    "score": 1,
    "metadata": {
        "Content-Type": "application/json"
    },
    "enriched_field_units": 0
}

Is there something simple i miss?

2

There are 2 answers

0
Joshua Smith On BEST ANSWER

You didn't miss anything. There is a bug in the SDK that's being worked on right now. It should be fixed soon. https://github.com/watson-developer-cloud/node-sdk/issues/369

0
Nathan Friedly On

I know this is an old question, but I wanted to provide an update now that a couple of additional bugs have been fixed and a new addJsonDocument() method was added in v2.34.0.

The first thing to point out is that Discovery ignores the metadata object when determining content type. It looks at the filename and the file contents only.

text/plain is not supported, so with your exact example, it returns a correct identification and error message.

However if you substituted valid JSON there, it may still mis-identify it as text without an appropriate filename.

Setting a .json filename or using the new addJsonDocument() method would resolve the issue:

  • To set the filename, set file to an object with value and options.filename fields:

    discovery.addDocument({
        environment_id: 'env-id-here',
        collection_id: 'coll-id-here',
        configuration_id: 'config-id-here',
        file: {
          value: Buffer.from("JSON goes here", 'utf8'),
          options: {
            filename: 'whatever.json'
          }
        }
    }, function(err, data) {
        if (err) {
            console.error(err);
        } else {
            console.log(JSON.stringify(data, null, 2));
        }
    });
    
  • Or, for JSON, use addJsonDocument():

    var document_obj = {
      environment_id: environment,
      collection_id: collection,
      file: {"foo": "bar"}
    };
    
    discovery.addJsonDocument(document_obj, function (err, response) {
      if (err) {
        console.error(err);
      } else {
        console.log(JSON.stringify(response, null, 2));
      }
    });