[英]Express.js elasticsearch query not working
我正在尝试构建一个Express.js应用程序,该应用程序查询在其他服务器上运行的elasticsearch。 我创建了一个快速脚手架应用程序,唯一更改的是在routes/index.js
。 我将其更改为如下所示:
var express = require('express');
var router = express.Router();
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'http://SERVERDNS.com:9200/'
});
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
router.get('/search', function(req, res) {
var wut = client.search({
index:"test-papers-es",
type:"test-papers-es",
body: {query:
{"match": req.query}
}
});
console.log(wut);
res.send(wut);
console.log(wut);
console.log(req.query);
});
module.exports = router;
问题是,当我发送如下查询时:
http://SERVERDNS.com:3000/search?title=gene
我没有得到回应(我期望使用JSON)。 我的client.search( var wut
)代码的console.log
是这样的:
{ _bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined,
_boundTo: undefined,
abort: [Function: abortRequest] }
我的req.query
看起来像这样: { title: 'gene' }
知道我做错了什么吗?
.search
不同步。 您不能只是调用它并期望下一行给出答案。 API使用承诺,如此处所述: http : //www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/quick-start.html#_say_hello_to_elasticsearch
举个例子:
client.search({
q: 'pants'
}).then(function (body) {
var hits = body.hits.hits;
}, function (error) {
console.trace(error.message);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.