0

I have tried a successful approach in Watson Retrieve and Rank, to set the 'ranker_id' from within the code snippet versus setting it as an environment variable.

Below is the code snippet:

        var qs = require('querystring');
        // search documents
        var ranker_id = 'replace with ID'; 
        var question = payload.input.text; //Only the question is required


        var query = **qs.stringify({q: question, ranker_id: ranker_id, fl: 'id,title,contentHtml'});**


        solrClient.get('fcselect', query, function(err, searchResponse) {...}.

In some versions of npm, qs also works- var qs = require('qs');

**This would be the requirement to deploy in all production architecture, where the code would reside in production servers & make calls to the API. In such a scenario, env variable(ranker_id) could not be set in production environment, hence this approach

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53

1 Answers1

0

You can see in this documentation here and see the all examples how to use Retrieve and Rank - IBM Watson.

In this case the password and username is Service Credentials.

One example search and rank:

var watson  = require('watson-developer-cloud');
var retrieve_and_rank = watson.retrieve_and_rank({
  username: '{username}',
  password: '{password}',
  version: 'v1'
});

var params = {
  cluster_id: 'sc1ca23733_faa8_49ce_b3b6_dc3e193264c6',
  collection_name: 'example_collection'
};

//  Use a querystring parser to encode output.
var qs = require('qs');

// Get a Solr client for indexing and searching documents.
// See https://github.com/watson-developer-cloud/node-sdk/blob/master/services/retrieve_and_rank/v1.js
solrClient = retrieve_and_rank.createSolrClient(params);

var ranker_id = 'B2E325-rank-67'; //PASTE YOUR RANKER_ID
var question  = 'what is the basic mechanism of the transonic aileron buzz';
var query     = qs.stringify({q: question, ranker_id: ranker_id, fl: 'id,title'});

solrClient.get('fcselect', query, function(err, searchResponse) {
  if(err) {
    console.log('Error searching for documents: ' + err);
  }
    else {
      console.log(JSON.stringify(searchResponse.response.docs, null, 2));
    }
});

See one example how Get information about a ranker:

var watson = require('watson-developer-cloud');
var retrieve_and_rank = watson.retrieve_and_rank({
  username: '{username}',  //username from Service Credentials Retrieve and Rank
  password: '{password}', // password from Service Credentials Retrieve and Rank
  version: 'v1'
});

var params = {
  ranker_id: 'B2E325-rank-67', //PASTE YOUR RANKER_ID
};

retrieve_and_rank.rankerStatus(params,
function(err, response) {
if (err)
  console.log('error:', err);
else
  console.log(JSON.stringify(response, null, 2));
});

Example Index documents:

//require watson
var watson = require('watson-developer-cloud');

//call with your password and username from Service Retrieve and Rank Credentials
var retrieve_and_rank = watson.retrieve_and_rank({
  username: '{username}',
  password: '{password}',
  version: 'v1'
});
//cluster id from your documentation
var params = {
  cluster_id: 'sc1ca23733_faa8_49ce_b3b6_dc3e193264c6',
  collection_name: 'example_collection',
};
// your doc here
var doc = {
    id: 1,
    author: 'brenckman,m.',
    bibliography: 'j. ae. scs. 25, 1958, 324.',
    body: 'experimental investigation of the aerodynamics of a wing in a slipstream.   an experimental study of a wing in a propeller slipstream was made in order to determine the spanwise distribution of the lift increase due to slipstream at different angles of attack of the wing and at different free stream to slipstream velocity ratios.',
    title: 'experimental investigation of the aerodynamics of a wing in a slipstream'
};

//Get a Solr client for indexing and searching documents with rankerid inside params variable
solrClient = retrieve_and_rank.createSolrClient(params);

console.log('Indexing a document...');
solrClient.add(doc, function (err, response) {
  if (err) {
    console.log('Error indexing document: ', err);
  }
    else {
      console.log('Indexed a document.');
      solrClient.commit(function(err) {
        if(err) {
          console.log('Error committing change: ' + err);
        }
          else {
            console.log('Successfully committed changes.');
          }
      });
    }
});
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53