我有全文搜索,返回 Keystone 模型的结果。问题是我使用 model.find() 生成这些结果。我想对这些结果进行分页,但找不到将 $text
与 .paginate()
一起使用的方法。有没有办法将 mongoose 全文搜索与 Keystone 分页集成,甚至对 model.find()
的结果进行分页?
编辑
我已经厌倦了使用 keystone.list('List').paginate().find()
但收到错误:“回调不是函数。”也许我的代码还有其他问题:
keystone.list('Document').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 6,
})
.find(
{ $text : { $search : req.query.search } },
{ score : { $meta: "textScore" } }
)
.sort({ score : { $meta : 'textScore' } })
.exec(function(err, results) {
locals.documents = results;
next();
});
最佳答案
这就是我设法做过滤器的方法。 Keystone boilerplate 。下面是我的待办事项模型
import keystone from 'keystone';
const { Field: { Types } } = keystone;
const Todo = new keystone.List('Todo');
Todo.add({
title: {
type: String, required: true, default: Date.now,
},
description: { type: Types.Html, wysiwyg: true },
createdAt: { type: Date, default: Date.now },
});
Todo.relationship({ path: 'users', ref: 'User', refPath: 'todos' });
Todo.schema.index({ title: 'text' });
Todo.register();
export default Todo;
和分页过滤器
export const list = (req, res) => {
const search = new RegExp(req.query.search || '', 'i');
const query = {
title: search,
};
Todo.paginate({
page: req.query.page || 1,
perPage: 5,
maxPages: 10,
filters: {
$or: [query],
},
}).sort('-createdAt').exec((err, items) => {
if (err) return res.status(500).json(err);
return res.json({
data: items,
});
});
};
关于mongoose - 使用 mongoose 全文搜索和 KeystoneJS 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059600/