[英]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. 这是我第一次使用ElasticSearch,而Node和MongoDB也是我的新手。
So, I followed a kind of tutorial and implemented Mongooastic from NPM to let ElasticSearch work with Node. 因此,我遵循了一种教程并从NPM实施了Mongooastic,以使ElasticSearch与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... 即使在总共12个被索引的用户上,它似乎也可以正常工作,如果我在搜索列表视图中键入搜索“ user”,我可以找到12条记录,则每条显示10条记录,而第一个记录缺少值。 ..
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
我真的不知道在哪里放置size
以及from
这里开始。在理解了这一点之后,我还想知道如何实现(如果可能)一种无限滚动...以及如何处理链接。分页...即: 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... 该视图有一个简单的输入文本供搜索,按提交后,它会打开一个包含hits
列表的新页面,因此应该没有什么复杂的...
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. 默认情况下,elasticsearch返回前10个结果:如果需要更多则必须设置size参数。
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 ) (参见这里: 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 ). 这将使您的用户n°30达到用户n°60(此处有更多信息: http : //www.elasticsearch.org/guide/zh-CN/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. 您将必须使用前端才能从 大小和参数中获取所需的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.