elasticsearch,Json,Node.js,Express,elasticsearch" /> elasticsearch,Json,Node.js,Express,elasticsearch" />

在node.js中获取json输出(使用elasticsearch和express作为web框架)

在node.js中获取json输出(使用elasticsearch和express作为web框架),json,node.js,express,elasticsearch,Json,Node.js,Express,elasticsearch,我正在使用node.js在elasticsearch的基础上构建一个搜索引擎web应用程序。我在elasticsearch中使用sense为一个网站编制了索引,现在在express中使用我的索引构建了一个网页 这是我的javascript: var elasticsearch = require('elasticsearch'); var client = elasticsearch.Client({ hosts: [ 'localhost:9200' ] }); modul

我正在使用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);
  });
}
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;
这是我的地址:

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);
  });
}
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>

搜寻

我得到的网页如下所示:


如果我正在搜索一个查询,我将获得我搜索的查询的标题。但它不是json格式的。如果我们进行查询,我希望我的网页打印与elasticsearch相同的结果(JSON格式)。

结果显示为
JSON数据的一种简单方法是在
ejs
模板中使用

例如:

<!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这不是一个打字错误
是的,对不起我的错(生锈了!),谢谢你的纠正。