Skip to content

Commit b59d386

Browse files
committed
init
1 parent de4a179 commit b59d386

32 files changed

+6642
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ node_modules
2828

2929
# Debug log from npm
3030
npm-debug.log
31+
32+
.env

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Node Elasticsearch Example
2+
3+
## Database Structure and Initialization
4+
5+
Please check `data/sample_data.sql` for our sample data.
6+
7+
8+
## Elastic Integration
9+
10+
First of all create your `products` index and `product` type.
11+
12+
```
13+
DELETE products
14+
15+
PUT products
16+
{
17+
"settings": {
18+
"number_of_shards": 1,
19+
"number_of_replicas": 0
20+
}
21+
}
22+
23+
24+
PUT products/product/_mapping
25+
{
26+
"properties": {
27+
"name": {
28+
"type": "text"
29+
},
30+
"description": {
31+
"type": "text"
32+
},
33+
"quantity": {
34+
"type": "long"
35+
},
36+
"price": {
37+
"type": "double"
38+
},
39+
"created_at": {
40+
"type": "date"
41+
},
42+
"updated_at": {
43+
"type": "date"
44+
},
45+
"categories": {
46+
"type": "object",
47+
"properties": {
48+
"id": {
49+
"type": "long"
50+
},
51+
"name": {
52+
"type": "text"
53+
}
54+
}
55+
}
56+
}
57+
}
58+
```
59+
60+
Then check `data/logstash.conf` file and also last part of the
61+
`data/sample_data.sql`. There is a procedure in there.
62+
63+
```
64+
DROP PROCEDURE fetchDataForElastic;
65+
66+
DELIMITER //
67+
CREATE PROCEDURE fetchDataForElastic
68+
(IN currentdate Datetime)
69+
BEGIN
70+
SELECT
71+
p.*,
72+
73+
CAST( (CONCAT ('[', GROUP_CONCAT(CONCAT('{"id":', c.id, ', "name":"',c.name,'"}')), ']')) AS JSON) categories
74+
FROM products p LEFT JOIN product_category pc ON pc.product_id = p.id LEFT JOIN categories c ON c.id = pc.category_id
75+
WHERE p.updated_at > currentdate GROUP BY p.id;
76+
END //
77+
DELIMITER ;
78+
```
79+
80+
At the end, run logstash. `docker-compose up logstash`

app.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
var lessMiddleware = require('less-middleware');
8+
9+
// .env file configuration
10+
require('dotenv').config();
11+
12+
13+
var index = require('./routes/index');
14+
var products = require('./routes/products');
15+
var categories = require('./routes/categories');
16+
var search = require('./routes/search');
17+
var graphs = require('./routes/graphs');
18+
19+
var app = express();
20+
21+
// view engine setup
22+
app.set('views', path.join(__dirname, 'views'));
23+
app.set('view engine', 'twig');
24+
25+
// uncomment after placing your favicon in /public
26+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
27+
app.use(logger('dev'));
28+
app.use(bodyParser.json());
29+
app.use(bodyParser.urlencoded({ extended: false }));
30+
app.use(cookieParser());
31+
app.use(lessMiddleware(path.join(__dirname, 'public')));
32+
app.use(express.static(path.join(__dirname, 'public')));
33+
34+
app.use('/', index);
35+
app.use('/product', products);
36+
app.use('/category', categories);
37+
app.use('/search', search);
38+
app.use('/graphs', graphs);
39+
40+
// catch 404 and forward to error handler
41+
app.use(function(req, res, next) {
42+
var err = new Error('Not Found');
43+
err.status = 404;
44+
next(err);
45+
});
46+
47+
// error handler
48+
app.use(function(err, req, res, next) {
49+
// set locals, only providing error in development
50+
res.locals.message = err.message;
51+
res.locals.error = req.app.get('env') === 'development' ? err : {};
52+
53+
// render the error page
54+
res.status(err.status || 500);
55+
res.render('error');
56+
});
57+
58+
module.exports = app;

bin/www

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('node-es-example:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

data/logstash.conf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
input {
2+
jdbc {
3+
jdbc_driver_library => "mysql-connector-java-5.1.36-bin.jar"
4+
jdbc_driver_class => "com.mysql.jdbc.Driver"
5+
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/node_es_example"
6+
jdbc_user => "root"
7+
jdbc_password => ""
8+
schedule => "* * * * *"
9+
statement => "CALL fetchDataForElastic(:sql_last_value);"
10+
}
11+
}
12+
13+
filter {
14+
json {
15+
source => "categories"
16+
target => "categories"
17+
}
18+
mutate { remove_field => [ "@version", "@timestamp" ] }
19+
}
20+
21+
output {
22+
stdout
23+
{
24+
codec => rubydebug
25+
}
26+
27+
elasticsearch
28+
{
29+
hosts => ["elasticsearch:9200"]
30+
index => "products"
31+
document_type => "product"
32+
document_id => "%{id}"
33+
}
34+
}

0 commit comments

Comments
 (0)