I am using node.js to build a search engine web app on top of elasticsearch. I have indexed a website in my elasticsearch using sense and now using my index in express to build a webpage.
This is my javascript:
var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
module.exports.search = function(searchData, callback) {
client.search({
index: 'demoindex1',
type: 'SearchTech',
body: {
query: {
bool: {
must: {
match: {
"newContent": searchData.searchTerm
}
}
}
}
}
}).then(function (resp) {
callback(resp.hits.hits);
}, function (err) {
callback(err.message)
console.log(err.message);
});
}
This is my routes javascript:
var express = require('express');
var router = express.Router();
var searchModule = require('../search_module/search.js');
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.post('/search-results', function(req, res) {
searchModule.search(req.body, function(data) {
res.render('index', { title: 'Express', results: data });
});
});
module.exports = router;
This is my ejs file which I'm using to create web page.
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1><%= title %></h1>
<form action='/search-results' method='post'>
<input type="text" name="searchTerm" placeholder="your search term here">
<button type="submit"> SEARCH </button>
</form>
<ul>
<% if(locals.results) { %>
<% results.forEach( function( result ) { %>
<li>
<%= result._source.title %>
<br><%= result._source.U %>
</li>
<% }) %>
<% } %>
</ul>
</body>
</html>
The webpage I am getting looks this: https://i.sstatic.net/w8dVE.png
If I am searching for a query, I am getting a title of the query that I searched for. But it is not in json form. I would like my web page to print the same result(JSON form) that we get in elasticsearch if we do a query.