如何从nodeJs代码中调用/使用排名器ID来调用Retrieve&Rank API?



我在 Watson Retrieve and Rank 中尝试了一种成功的方法,从代码片段中设置"ranker_id",而不是将其设置为环境变量。

下面是代码片段:

        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) {...}.

在某些版本的 npm 中,qs 也可以工作- var qs = require('qs'(;

**这将是在所有生产架构中部署的要求,其中代码将驻留在生产服务器中并调用 API。在这种情况下,无法在生产环境中设置 env 变量 (ranker_id(,因此这种方法

您可以在此处查看此文档,并查看如何使用检索和排名 - IBM Watson 的所有示例。

在这种情况下,密码和用户名是服务凭据

一个示例搜索和排名:

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));
    }
});

请参阅一个示例,了解如何获取有关排名器的信息:

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));
});

示例索引文档:

//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.');
          }
      });
    }
});

最新更新