Skip to content

Commit 6ab6b78

Browse files
committed
Add R&R example for indexing Solr docs from a JSON file
1 parent 47886d5 commit 6ab6b78

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

examples/resources/solr_docs.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[{ "id" : 111, "title_s" : "JSON doc 1", "text_field_s": "one" },
2+
{ "id" : 222, "title_s" : "JSON doc 2", "text_field_s": "two" }]

examples/retrieve_and_rank_solr.v1.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var watson = require('watson-developer-cloud');
44
var async = require('async');
5+
var fs = require('fs');
56

67
var retrieve = watson.retrieve_and_rank({
78
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
@@ -14,6 +15,7 @@ var clusterId = 'INSERT YOUR CLUSTER ID HERE';
1415
var collectionName = 'example_collection';
1516
var configName = 'example_config';
1617
var configZipPath = __dirname + '/resources/example_solr_config.zip';
18+
var jsonDocsFilePath = __dirname + '/resources/solr_docs.json';
1719

1820
var solrClient = retrieve.createSolrClient({
1921
cluster_id: clusterId,
@@ -74,7 +76,28 @@ async.series([
7476
});
7577
},
7678

77-
function indexAndCommit(done) {
79+
function indexAndCommitJsonFileDocs(done) {
80+
console.log('Indexing documents via JSON file...');
81+
var jsonDocs = JSON.parse(fs.readFileSync(jsonDocsFilePath, 'utf8'));
82+
solrClient.add(jsonDocs, function(err) {
83+
if(err) {
84+
console.log('Error indexing document: ' + err);
85+
done();
86+
} else {
87+
console.log('Indexed JSON documents.');
88+
solrClient.commit(function(err) {
89+
if(err) {
90+
console.log('Error committing change: ' + err);
91+
} else {
92+
console.log('Successfully commited changes.');
93+
}
94+
done();
95+
});
96+
}
97+
});
98+
},
99+
100+
function indexAndCommitDocObject(done) {
78101
console.log('Indexing a document...');
79102
var doc = { id : 1234, title_t : 'Hello', text_field_s: 'some text' };
80103
solrClient.add(doc, function(err) {

0 commit comments

Comments
 (0)