-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-cloudsearch.js
96 lines (68 loc) · 2.12 KB
/
aws-cloudsearch.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
module.exports = function(RED) {
function AwsCloudSearchNode(config) {
RED.nodes.createNode(this, config);
var node = this;
// Retrieve the config node
var cloudsearch = RED.nodes.getNode(config.cloudsearch);
var awsConfig = RED.nodes.getNode(cloudsearch.awsConfig);
this.endpoint = cloudsearch.endpoint;
this.apiVersion = cloudsearch.apiVersion;
this.CloudSearchDomain = undefined;
this.awsConfig = {
accessKeyId: awsConfig.accessKeyId,
secretAccessKey: awsConfig.secretAccessKey,
region: awsConfig.region
};
if(this.awsConfig) {
const AWS = require('aws-sdk');
AWS.config.update(this.awsConfig);
AWS.config.apiVersions = {
cloudsearchdomain: this.apiVersion,
// cloudsearchdomain: '2013-01-01',
};
this.CloudSearchDomain = new AWS.CloudSearchDomain({ endpoint: this.endpoint });
if(!this.CloudSearchDomain) {
return;
}
node.on('input', function(msg) {
var params = msg.params || getParamObject(msg);
this.CloudSearchDomain.search(params, function(err, data) {
if(err) {
node.error("Search failed " + err.toString(), err.stack);
}
msg.payload = [err, data];
node.send(msg);
});
});
} else {
// No config node configured
node.error("No node configured");
}
}
function getParamObject(msg) {
var supportedKeys = ['query', 'facet', 'queryParser', 'size', 'start', 'filterQuery', 'highlight', 'partial', 'queryOptions', 'return', 'sort', 'stats'];
var params = {};
supportedKeys.forEach(e => {
if(msg[e] !== undefined) {
params[e] = msg[e];
}
}
);
return params;
}
RED.nodes.registerType("aws-cloudsearch", AwsCloudSearchNode);
function AWSConfigNode(n) {
RED.nodes.createNode(this, n);
this.accessKeyId = n.accessKeyId;
this.secretAccessKey = n.secretAccessKey;
this.region = n.region;
}
RED.nodes.registerType("aws-config", AWSConfigNode);
function AWSCloudSearchConfigNode(n) {
RED.nodes.createNode(this, n);
this.endpoint = n.endpoint;
this.apiVersion = n.apiVersion;
this.awsConfig = n.aws;
}
RED.nodes.registerType("aws-cloudsearch-config", AWSCloudSearchConfigNode);
}