我在Watson 检索和排名 中尝试了成功的方法,在代码段中设置'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),因此这种方法
答案 0 :(得分:0)
您可以在此处查看this文档,并查看所有示例如何使用Retrieve和Rank - 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));
}
});
查看一个example如何获取有关排名的信息:
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.');
}
});
}
});