forked from aws-samples/lambda-refarch-mobilebackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (59 loc) · 1.59 KB
/
index.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
var AWS = require('aws-sdk');
var doc = new AWS.DynamoDB.DocumentClient();
var config;
exports.handler = function(event, context) {
if (config) {
handleEvent(event, context);
} else {
var params = {
TableName: 'MobileRefArchConfig',
Key: { Environment: 'demo' }
};
doc.get(params, function(err, data) {
if (err) {
console.log(err, err.stack);
context.fail(err);
} else {
config = data.Item;
handleEvent(event, context);
}
});
}
};
function handleEvent(event, context) {
var cloudSearchDomain = new AWS.CloudSearchDomain({
endpoint: config.SearchEndpoint
});
var params = {
query: event.searchTerm,
size: 10,
start: 0
};
cloudSearchDomain.search(params, function(err, data) {
if (err) {
context.fail(new Error('Error searching for documents with term: "' + event.searchTerm + '"')); // an error occurred
} else {
context.succeed(processSearchResults(data));
}
});
}
function processSearchResults(data) {
//Set base response type
var response = {
success: true
};
var searchResults = [];
var hits = data.hits.hit;
for (var i = 0; i < hits.length; i++) {
//retrieve the next notes
var currentMatch = hits[i];
var searchResult = {};
//Configure each note and push onto the search results
searchResult.noteId = currentMatch.id;
searchResult.headline = currentMatch.fields.headline[0];
searchResult.text = currentMatch.fields.note_text[0];
searchResults.push(searchResult);
}
response.notes = searchResults;
return response;
}