1

I tried to search online for a solution but to be honest I still didn't found anything that help me to achieve this.

It is the first time I use ElasticSearch and I'm also pretty new with Node and MongoDB.

So, I followed a kind of tutorial and implemented Mongooastic from NPM to let ElasticSearch work with Node.

It seems to work fine even if on a total of 12 users indexed, if I type in the search "user" in the search list view I can find 12 records, it show 10 in a for each and the first one has missing values...

But the main problem for me is the pagination... or a sort of... it will be also nice to implement infinite scroll on it but I don't really know how to handle it.

So, the controller that handle it is the following at the moment:

exports.searchUsers = function(req, res) {
    User.search({
      query_string: {
        query: req.query.q
      }
    },
    { hydrate: true },
    function(err, results) {
      if (err) res.send(err);
      res.render('search-results', {
        results: results,
        users: results.hits.hits
      });
    });
};

I don't really know where to put size and from in here... and after understanding this I also would like to know how to implement, if possible, a sort of infinite scroll... And also how to handle link for the pagination... i.e.: prev, 1, 2, 3, 4, ..., next

The view has a simple input text for the search, after pressing submit it open a new page with the list of hits, so it should be nothing complex...

I hope you may help. Thanks

1 Answer 1

2

By default elasticsearch returns the 10 first results: you will have to set the size parameter if you want more.

Also, in order to add the size and from parameters, you need to write your query like so:

User.search({
    query: {
      query_string: {
        query: req.query.q
      }
    },
    size: 30,
    from: 30
  },
  {hydrate: true},
  function (err, results) {
    if (err) res.send(err);
    res.render('search-results', {
      results: results,
      users: results.hits.hits
    });
  });

(see here: https://github.com/taterbase/mongoosastic/issues/123 )

This will give you user n°30 up to user n°60 (more info here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html). You will have to play with your frontend to get the values you want for your size and from parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

awesome! Do you know if it is also possible to do infinite scroll with it in someway?
Sorry but I tried it right now and it doesn't work at all. I receive the following error: All shards failed for phase: [query]
Weird... Can you paste the whole error message here?

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.