简体   繁体   中英

Elasticsearch pagination with MongoDB and ExpressJS

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... ie: 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM