Skip to content

Commit 5ad5890

Browse files
authored
Merge pull request #231 from chughts/disquery
Discovery Query Builder
2 parents f7ba9d6 + eef0b2c commit 5ad5890

File tree

7 files changed

+849
-27
lines changed

7 files changed

+849
-27
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Node-RED Watson Nodes for IBM Bluemix
77

88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

10+
### New in version 0.4.31
11+
- New V1 Query Builder Node for the Discovery Node
12+
1013
### New in version 0.4.30
1114
- New services utilities to handle node-red app name clash problems
1215
- New V1 Discovery Node

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.4.30",
3+
"version": "0.4.31",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"alchemy-api": "^1.3.0",
@@ -35,6 +35,7 @@
3535
"watson-conversation-v1":"services/conversation/v1.js",
3636
"watson-conversation-v1-experimental":"services/conversation/v1-exp.js",
3737
"watson-discovery-v1":"services/discovery/v1.js",
38+
"watson-discovery-v1-query-builder":"services/discovery/v1-query-builder.js",
3839
"watson-discovery-v1-experimental":"services/discovery/v1-exp.js",
3940
"watson-document-conversion-v1": "services/document_conversion/v1.js",
4041
"watson-concept-insights-v2": "services/concept_insights/v2.js",

services/discovery/discovery-utils.js

+95-25
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
function DiscoveryUtils() {}
1818
DiscoveryUtils.prototype = {
1919

20-
buildParams: function(msg, config) {
21-
var params = {};
20+
buildParamsForName: function(msg, config, params) {
2221
if (msg.discoveryparams && msg.discoveryparams.environmentname) {
2322
params.name = msg.discoveryparams.environmentname;
2423
} else if (config.environmentname) {
@@ -28,39 +27,63 @@ DiscoveryUtils.prototype = {
2827
} else if (config.configurationname) {
2928
params.name = config.cofigurationname;
3029
}
30+
return params;
31+
},
3132

32-
if (msg.discoveryparams && msg.discoveryparams.environment_id) {
33-
params.environment_id = msg.discoveryparams.environment_id;
34-
} else if (config.environment_id) {
35-
params.environment_id = config.environment_id;
33+
buildParamsFor: function(msg, config, params, field) {
34+
if (msg.discoveryparams && msg.discoveryparams[field]) {
35+
params[field] = msg.discoveryparams[field];
36+
} else if (config[field]) {
37+
params[field] = config[field];
3638
}
39+
return params;
40+
},
3741

38-
if (msg.discoveryparams && msg.discoveryparams.collection_id) {
39-
params.collection_id = msg.discoveryparams.collection_id;
40-
} else if (config.collection_id) {
41-
params.collection_id = config.collection_id;
42+
buildParamsFromConfig: function(config, params, field) {
43+
if (config[field]) {
44+
params[field] = config[field];
4245
}
46+
return params;
47+
},
4348

44-
if (msg.discoveryparams && msg.discoveryparams.configuration_id) {
45-
params.configuration_id = msg.discoveryparams.configuration_id;
46-
} else if (config.configuration_id) {
47-
params.configuration_id = config.configuration_id;
48-
}
49+
buildParams: function(msg, config) {
50+
var params = {};
51+
52+
params = DiscoveryUtils.prototype.buildParamsForName(msg, config, params);
53+
54+
['environment_id','collection_id','configuration_id','query'].forEach(function(f) {
55+
params = DiscoveryUtils.prototype.buildParamsFor(msg, config, params, f);
56+
});
57+
58+
['count','filter','aggregation','return'].forEach(function(f) {
59+
params = DiscoveryUtils.prototype.buildParamsFromConfig(config, params, f);
60+
});
4961

50-
if (config.count) {
51-
params.count = config.count;
62+
return params;
63+
},
64+
65+
buildMsgOverrides: function(msg, config) {
66+
var params = {};
67+
if (config.environment) {
68+
params.environment_id = config.environment;
5269
}
53-
if (config.query) {
54-
params.query = config.query;
70+
if (config.collection) {
71+
params.collection_id = config.collection;
5572
}
56-
if (config.filter) {
57-
params.filter = config.filter;
73+
if (config.query1 && config.queryvalue1) {
74+
params.query = config.query1 + ':"' + config.queryvalue1 + '"';
5875
}
59-
if (config.aggregation) {
60-
params.aggregation = config.aggregation;
76+
if (config.query2 && config.queryvalue2) {
77+
if (params.query) {
78+
params.query += ',';
79+
}
80+
params.query += config.query2 + ':"' + config.queryvalue2 + '"';
6181
}
62-
if (config.return) {
63-
params.return = config.return;
82+
if (config.query3 && config.queryvalue3) {
83+
if (params.query) {
84+
params.query += ',';
85+
}
86+
params.query += config.query3 + ':"' + config.queryvalue3 + '"';
6487
}
6588

6689
return params;
@@ -90,6 +113,53 @@ DiscoveryUtils.prototype = {
90113
return response;
91114
},
92115

116+
// Looking for Text, Type and label
117+
buildFieldByStep: function(d, fields, txt) {
118+
for (var k in d) {
119+
var t = txt;
120+
if (isNaN(k)) {
121+
t += txt ? '.' : '';
122+
t += k;
123+
}
124+
125+
if ('object' === typeof d[k]) {
126+
fields = DiscoveryUtils.prototype.buildFieldByStep(d[k], fields, t);
127+
} else {
128+
switch (k) {
129+
case 'text':
130+
case 'type':
131+
case 'label':
132+
fields.push(t);
133+
break;
134+
}
135+
}
136+
}
137+
return fields;
138+
},
139+
140+
// sorting functions
141+
uniqueFilter: function (value, index, self) {
142+
return self.indexOf(value) === index;
143+
},
144+
145+
// Looking for Text, Type and label
146+
buildFieldList: function(schemaData) {
147+
var fields = [];
148+
if ('object' === typeof schemaData) {
149+
for (var k in schemaData) {
150+
if ('results' === k &&
151+
'object' === typeof schemaData[k] &&
152+
'object' === typeof schemaData[k][0]) {
153+
fields = DiscoveryUtils.prototype.buildFieldByStep(schemaData[k][0], fields, '');
154+
}
155+
}
156+
if (fields.length) {
157+
fields = fields.filter(DiscoveryUtils.prototype.uniqueFilter);
158+
}
159+
}
160+
return fields;
161+
},
162+
93163
reportError: function (node, msg, message) {
94164
var messageTxt = message.error ? message.error : message;
95165
node.status({fill:'red', shape:'dot', text: messageTxt});

0 commit comments

Comments
 (0)