1

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.

1 Answer 1

1

A simple way to displaying the results as json data is by using stringify in the ejs template

Example:

<!DOCTYPE html>
<html>
  <head>
    <title>hello</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>
      <% if(locals.results) { %>
        <pre>
           <%- JSON.stringify(results,null,2) %>
        </pre>
      <% } %> 
  </body>
</html>
Sign up to request clarification or add additional context in comments.

3 Comments

@keety Thanks. Now I am getting JSON output of elasticsearch.
@Val that is not a typo <%- is used to display unescaped html
Yes, sorry my bad (rusty me!), thanks for correcting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.