This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
121 lines (97 loc) · 2.94 KB
/
server.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
'use strict';
let express = require('express');
let app = express();
const elasticsearch = require('elasticsearch');
let esclient = new elasticsearch.Client({host: 'localhost:9200'});
let url = require('url');
/**
* The endpoint that will deliver search results.
*/
app.get('/api/search', function (req, res) {
// Get query parameters
let url_parts = url.parse(req.url, true);
let query = url_parts.query;
// If no query was specified, return nothing.
if (!query.input) {
res.json({
total: 0,
hits: [],
});
return;
}
// TODO: improve this query.
let searchQuery = {
index: 'openrecipes',
body: {
query: {
match: {
'_all': {
query: query.input,
}
}
},
// TODO: add aggregations.
}
};
if (query.ingredients) {
if (typeof query.ingredients == 'string') {
query.ingredients = [query.ingredients];
}
// TODO: add the ingredients from the request to filter the query.
}
if (query.sources) {
if (typeof query.sources == 'string') {
query.sources = [query.sources];
}
// TODO: add the sources from the request to filter the query.
}
// Add paging information to the query.
searchQuery.from = query.from || 0;
esclient.search(searchQuery).then(function(result) {
res.json({
total: result.hits.total,
hits: result.hits.hits.map((hit) => hit._source),
sourceFacet: [{key: 'bacon', doc_count: 10000}], // TODO get this data from an aggregation.
ingredientsFacet: [{key: 'tastykitchen', doc_count: 10000}], // TODO get this data from an aggregation.
});
}).catch(console.log)
});
/**
* The endpoint that will deliver search suggestions.
*/
app.get('/api/suggest', function (req, res) {
// Get query parameters
let url_parts = url.parse(req.url, true);
let query = url_parts.query;
// If no query was specified, return nothing.
if (!query.input) {
res.json([]);
return;
}
// TODO: Build this query.
// let searchQuery = {
// index: 'openrecipes',
// size: 0,
// body: {...}
// };
// TODO: remove this stub.
res.json([
{label: 'Bacon'},
{label: 'More bacon'},
]);
// TODO: uncomment the lines below to execute the query and return the results.
// esclient.search(searchQuery).then(function(result) {
// res.json(result.aggregations.ingredients.buckets.map((bucket) => ({label: bucket.key})));
// }).catch(console.log)
});
app.use(express.static('public'));
app.use(express.static('node_modules/bootstrap/dist'));
app.use(express.static('node_modules/angular'));
app.use(express.static('node_modules/angular-aria'));
app.use(express.static('node_modules/angular-animate'));
app.use(express.static('node_modules/angular-material'));
app.use(express.static('node_modules/angular-messages'));
app.use(express.static('node_modules/angular-resource'));
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});