Express.js elasticsearch查詢不起作用

[英]Express.js elasticsearch query not working


I'm trying to build an Express.js application that queries my elasticsearch running on a different server. I created an express scaffold app, and the only thing I've changed is in routes/index.js. I changed it to look like the following:

我正在嘗試構建一個Express.js應用程序,用於查詢在不同服務器上運行的彈性搜索。我創建了一個快速腳手架應用程序,我唯一改變的是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;

The problem is, when I send a query like so:

問題是,當我發送這樣的查詢時:

http://SERVERDNS.com:3000/search?title=gene

I don't get a response (I'm expecting JSON). The console.log for my client.search(var wut) code is this:

我沒有得到回復(我期待JSON)。我的client.search(var wut)代碼的console.log是這樣的:

{ _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined,
  _settledValue: undefined,
  _boundTo: undefined,
  abort: [Function: abortRequest] } 

And my req.query looks like this:{ title: 'gene' }

我的req.query看起來像這樣:{title:'gene'}

Any idea of what I did wrong?

知道我做錯了什么嗎?

1 个解决方案

#1


3  

.search is not synchronous. You can't just invoke it and expect an answer the next line. The API uses promises, as mentioned here: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/quick-start.html#_say_hello_to_elasticsearch

.search不同步。你不能只是調用它並希望在下一行得到答案。 API使用promises,如下所述:http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/quick-start.html#_say_hello_to_elasticsearch

As an example:

舉個例子:

client.search({
  q: 'pants'
}).then(function (body) {
  var hits = body.hits.hits;
}, function (error) {
  console.trace(error.message);
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/02/20103bea9a2344c241ae01026439294f.html



 
  © 2014-2022 ITdaan.com 联系我们: