From d243f31f7d245efce4bd857b32dc758205c28758 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 8 Nov 2018 18:19:13 +0100 Subject: Improved search algorithms --- app.js | 6 ++++-- database.js | 4 ++-- routes/index.js | 18 ------------------ routes/search.js | 20 ++++++++++++++++++++ routes/suggest.js | 9 +++++++++ 5 files changed, 35 insertions(+), 22 deletions(-) delete mode 100644 routes/index.js create mode 100644 routes/search.js create mode 100644 routes/suggest.js diff --git a/app.js b/app.js index 761d682..901fb86 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,8 @@ const path = require('path'); const cookieParser = require('cookie-parser'); const logger = require('morgan'); -const indexRouter = require('./routes/index'); +const searchRouter = require('./routes/search'); +const suggestRouter = require('./routes/suggest'); const app = express(); @@ -14,7 +15,8 @@ app.use(express.urlencoded({extended: false})); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/', indexRouter); +app.use('/search', searchRouter); +app.use('/suggest', suggestRouter); // catch 404 and forward to error handler app.use(function (req, res, next) { diff --git a/database.js b/database.js index e080d39..f48f87e 100644 --- a/database.js +++ b/database.js @@ -5,7 +5,7 @@ const esClient = new elasticsearch.Client({ }); module.exports = { - index: function bulkIndex(index, type, data) { + index: (index, type, data) => { let bulkBody = []; data.forEach(item => { @@ -32,7 +32,7 @@ module.exports = { }) .catch(console.err); }, - search: function search(index, body) { + search: (index, body) => { return esClient.search({index: index, body: body}); } }; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js deleted file mode 100644 index 570aea0..0000000 --- a/routes/index.js +++ /dev/null @@ -1,18 +0,0 @@ -const database = require("../database"); -const express = require("express"); -const router = express.Router(); - -/* GET home page. */ -router.get('/', (req, res, next) => { - database.search("crawled", { - size: 20, - from: 0, - query: { - match_all: {} - } - }).then(results => { - res.json(results.hits) - }) -}); - -module.exports = router; \ No newline at end of file diff --git a/routes/search.js b/routes/search.js new file mode 100644 index 0000000..c861f61 --- /dev/null +++ b/routes/search.js @@ -0,0 +1,20 @@ +const database = require("../database"); +const express = require("express"); +const router = express.Router(); + +router.get('/', (req, res, next) => { + database.search("crawled", { + size: 20, + from: 0, + query: { + multi_match: { + query: req.query.q, + fuzziness: 3 // decrease for more specific results + } + } + }).then(results => { + res.json(results.hits) + }) +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/suggest.js b/routes/suggest.js new file mode 100644 index 0000000..352619f --- /dev/null +++ b/routes/suggest.js @@ -0,0 +1,9 @@ +const database = require("../database"); +const express = require("express"); +const router = express.Router(); + +router.get("/", (req, res, next) => { + res.json({message: "NOT INTEGRATED YET!"}) +}); + +module.exports = router; \ No newline at end of file -- cgit v1.2.3