【问题标题】:Get a json output in node.js (Using elasticsearch and express as a web framework )在 node.js 中获取 json 输出(使用 elasticsearch 和 express 作为 Web 框架)
【发布时间】:2016-08-22 12:08:03
【问题描述】:

我正在使用 node.js 在 elasticsearch 之上构建一个搜索引擎 Web 应用程序。我已经在我的 elasticsearch 中使用 sense 为一个网站建立了索引,现在我在 express 中使用我的索引来构建一个网页。

这是我的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);
  });
}

这是我的路线 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;

这是我用来创建网页的 ejs 文件。

<!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>

我得到的网页是这样的: http://i.stack.imgur.com/w8dVE.png

如果我正在搜索查询,我会得到我搜索的查询的标题。但它不是json形式。如果我们进行查询,我希望我的网页打印与我们在 elasticsearch 中获得的相同结果(JSON 表单)。

【问题讨论】:

    标签: json node.js express elasticsearch


    【解决方案1】:

    results 显示为json data 的一种简单方法是在ejs 模板中使用stringify

    例子:

    <!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>
    

    【讨论】:

    • @keety 谢谢。现在我得到了 elasticsearch 的 JSON 输出。
    • @Val 不是拼写错误&lt;%- 用于显示unescaped html
    • 是的,对不起我的错(生锈了!),感谢您的纠正。
    猜你喜欢
    • 2014-08-07
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2021-08-15
    • 2021-11-30
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多