【发布时间】: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