Bluemix API of retrieve and rank in Node.js
Solr_Japanese_Chi_20160509
and train_with_encoded_ja
are specific for this purpose.$ npm install watson-developer-cloud --save
var retrieve_and_rank = watson.retrieve_and_rank({
username: '<username>',
password: '<password>',
version: 'v1'
});
function createCluster() {
retrieve_and_rank.createCluster({
cluster_size: '1',
cluster_name: 'API_version'
},
function (err, response) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(response, null, 2));
});
}
Upload configuration file
function uploadConfig() {
var params = {
cluster_id: cluster_id,
config_name: config_name,
config_zip_path: 'Solr_Japanese_Chi_20160509.zip'
};
retrieve_and_rank.uploadConfig(params,
function (err, response) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(response, null, 2));
});
}
Create a collection
function createCollection(name) {
var params = {
cluster_id: cluster_id,
config_name: config_name,
collection_name: name,
wt: 'json'
};
retrieve_and_rank.createCollection(params,
function (err, response) {
if (err)
console.log('error:', err);
else
console.log(JSON.stringify(response, null, 2));
});
}
Upload documents.
function uploadDoc(path) {
var file = path;
var doc = jsonfile.readFileSync(file);
solrClient = retrieve_and_rank.createSolrClient({
cluster_id: cluster_id,
collection_name: collection_name,
});
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.');
}
});
}
});
}
$ python ./train_with_encoded_ja.py -u "<username>":"<password>"
Search and Rank
function searching(input) {
solrClient = retrieve_and_rank.createSolrClient({
cluster_id: cluster_id,
collection_name: collection_name,
wt: 'json'
});
var ranker_id = '';
var question = 'q=' + input;
var query = qs.stringify({q: question, ranker_id: ranker_id, fl: 'id,body'});
solrClient.get('fcselect', query, function(err, searchResponse) {
if(err) {
console.log('Error searching for documents: ' + err);
}
else {
var result = JSON.stringify(searchResponse.response.docs, null, 2);
result = JSON.parse(result);
if (result == '') {
console.log('Not found');
} else {
console.log(result);
}
}
});
}
Read IBM Watson Developer Cloud to know concept and flow. Sign up/in on Bluemix and crate a Retreive and Rank service with usr/pwd
Install nodejs on your computer: https://nodejs.org/en/
Oepn your Terminal/Command Line Tool.
go to Project Path:
cd /path
Install modeules:
npm install
(if you encouter permission problems: sudo npm install)
Main functions are all implement in Solrcluster.js
file.
Enjoy it (Ask something).
Remember Close the sever by Ctl+c