1

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:

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:

{ _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' }

Any idea of what I did wrong?

2
  • What happens if you search via cURL? Can you find your values with it? Commented Aug 2, 2014 at 19:32
  • Gives this response: {"isFulfilled":false,"isRejected":false} Commented Aug 2, 2014 at 19:35

1 Answer 1

5

.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

As an example:

client.search({
  q: 'pants'
}).then(function (body) {
  var hits = body.hits.hits;
}, function (error) {
  console.trace(error.message);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Would I do something like res.send(hits); in order to get the hits back?
Basically yes, if you want them in JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.