forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve_and_rank_solr.v1.js
168 lines (148 loc) · 4.78 KB
/
retrieve_and_rank_solr.v1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var RetrieveAndRankV1 = require('watson-developer-cloud/retrieve-and-rank/v1');
var async = require('async');
var fs = require('fs');
var retrieve = new RetrieveAndRankV1({
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
});
var clusterId = 'INSERT YOUR CLUSTER ID HERE';
var collectionName = 'example_collection';
var configName = 'example_config';
var configZipPath = __dirname + '/resources/example_solr_config.zip';
var jsonDocsFilePath = __dirname + '/resources/solr_docs.json';
var solrClient = retrieve.createSolrClient({
cluster_id: clusterId,
collection_name: collectionName
});
async.series([
function uploadConfig(done) {
console.log('Uploading Solr config ' + configName);
retrieve.uploadConfig({
cluster_id: clusterId,
config_name: configName,
config_zip_path: configZipPath
},
function(err) {
printResponse(err, 'Error uploading Solr config: ', 'Uploaded Solr config ' + configName, done);
});
},
function listConfigs(done) {
console.log('Listing Solr configs for cluster ' + clusterId);
retrieve.listConfigs({cluster_id: clusterId}, function(err, res) {
printResponse(err, 'Error listing Solr configs: ', res, done);
});
},
function getConfig(done) {
console.log('Getting Solr config ' + configName);
retrieve.getConfig({
cluster_id: clusterId,
config_name: configName
}, function(err) {
if (err) {
console.log('Error getting config: ' + JSON.stringify(err, null, 2));
} else {
// Save response to a local file here
}
done();
});
},
function createCollection(done) {
retrieve.createCollection({
cluster_id: clusterId,
collection_name: collectionName,
config_name: configName
}, function(err, res) {
printResponse(err, 'Error creating Solr collection: ', res, done);
});
},
function listCollections(done) {
retrieve.listCollections({cluster_id: clusterId}, function(err, res) {
printResponse(err, 'Error listing Solr collections: ', res, done);
});
},
function indexAndCommitJsonFileDocs(done) {
console.log('Indexing documents via JSON file...');
var jsonDocs = JSON.parse(fs.readFileSync(jsonDocsFilePath, 'utf8'));
solrClient.add(jsonDocs, function(err) {
if(err) {
console.log('Error indexing document: ' + err);
done();
} else {
console.log('Indexed JSON documents.');
solrClient.commit(function(err) {
if(err) {
console.log('Error committing change: ' + err);
} else {
console.log('Successfully commited changes.');
}
done();
});
}
});
},
function indexAndCommitDocObject(done) {
console.log('Indexing a document...');
var doc = { id : 1234, title_t : 'Hello', text_field_s: 'some text' };
solrClient.add(doc, function(err) {
if(err) {
console.log('Error indexing document: ' + err);
done();
} else {
console.log('Indexed a document.');
solrClient.commit(function(err) {
if(err) {
console.log('Error committing change: ' + err);
} else {
console.log('Successfully commited changes.');
}
done();
});
}
});
},
function _search(done) {
console.log('Searching all documents.');
var query = solrClient.createQuery();
query.q({ '*' : '*' });
solrClient.search(query, function(err, searchResponse) {
if(err) {
console.log('Error searching for documents: ' + err);
} else {
console.log('Found ' + searchResponse.response.numFound + ' document(s).');
console.log('First document: ' + JSON.stringify(searchResponse.response.docs[0], null, 2));
}
done();
});
},
function deleteCollection(done) {
console.log('Deleting Solr collection ' + collectionName);
retrieve.deleteCollection({
cluster_id: clusterId,
collection_name: collectionName
}, function(err) {
printResponse(err, 'Error deleting collection: ', 'Deleted Solr collection ' + collectionName, done);
});
},
function deleteConfig(done) {
console.log('Deleting Solr config ' + configName);
retrieve.deleteConfig({
cluster_id: clusterId,
config_name: configName
}, function(err) {
printResponse(err, 'Error deleting config: ', 'Deleted Solr config ' + configName, done);
});
},
]);
function printResponse(error, errorMessage, response, callback) {
if (error) {
if (error.code) {
console.log(errorMessage + JSON.stringify(error, null, 2));
} else {
console.log(errorMessage + error);
}
} else {
console.log(response);
}
callback();
}