Search code examples
watson-discovery

Made a query in Watson discovery, tried to duplicate via nodejs sdk and the passages array is empty


After doing a simple natural language query in the build query page, set the options for "include relevant passages to Yes. I get back 5 passages and results. All good. When I try from npm ibm-watson 6 nodejs sdk. I get the results, but an empty passages array with the same natural langauge text.

Here is the the url from the query page showing the options, which I tried all of them

https://api.us-east.discovery.watson.cloud.ibm.com/instances/d45df72b-93e4-4897-b9ac-98dc1e088afc/v1/environments/xx/collections/xx/query?version=2018-12-03&deduplicate=false&highlight=true&passages=true&passages.count=5&natural_language_query=what%20is%20autism

Here is the answer from the query builder page enter image description here

Here is code example,

var discovery = new watson_discovery_v1({
  authenticator : new IamAuthenticator({apikey: msg.startup.discovery_password}),
  serviceUrl : msg.startup.discovery_endpoint,
  version: '2020-09-22'
});

msg.WDSParams = {
    environmentId: "x",
    collectionId: "x",
    passages: true,
    count:5,
    natural_language_query: msg.params.input.text
}



discovery.query(msg.WDSParams)
  .then(results => {
      msg.WDSResults = results; //your query results
      node.send(msg);
  })
  .catch(err => {
    console.log('error:', err);
  });

Here is the json that came back from the discovery call enter image description here

I have tried all of the passage options, duplicated the exact options that the query builder used. The same results come back, but no passages. Anyone have an idea? BTW using the Lite plan until I can prove passages works.


Solution

  • The problem was related to the way I called the query method. Below code resolved the issue. This code is for a nodeRed function node.

    const watson_discovery_v1 = global.get('watson_discovery_v1');
    const { IamAuthenticator } = global.get('ibm_auth');
    
    const discovery = new watson_discovery_v1({
      authenticator : new IamAuthenticator({apikey: 
      msg.startup.discovery_password}),
      serviceUrl : msg.startup.discovery_endpoint,
      version: '2019-04-30'
    });
    
      
    async function run() {
     try {
        const result = await discovery.query({
            environmentId: 'x',
            collectionId: 'x',
            passages: true,
            passagesCount: 2,
            count: 5,
            naturalLanguageQuery: msg.params.input.text
            })
    
        msg.WDSResults = result
        clearTimeout(myTM)
     }
     catch(e){
        node.error(e)
     }
       node.send(msg);
    }
    run()