是否有一个设置(或片段)我可以使用撇号搜索来执行部分字符串匹配的搜索?例如:搜索“蓝色”应返回标题为“学习管理蓝图”的项目。
2 回答
看起来需要添加正则表达式。如果其他人需要这个,这里是相关的 lib\modules\apostrophe-search\index.js 代码:
module.exports = {
perPage: 15,
construct: function(self, options) {
self.indexPage = function(req, callback) {
req.query.search = req.query.search || req.query.q;
var allowedTypes;
var defaultingToAll = false;
var cursor = self.apos.docs.find(req, { lowSearchText: new RegExp(req.query.search, 'i') } )
.perPage(self.perPage);
if (self.filters) {
var filterTypes = _.filter(
_.pluck(self.filters, 'name'),
function(name) {
return name !== '__else';
}
);
如你所知,我是 P'unk Avenue 的 Apostrophe 的首席开发人员。
您的解决方案确实有效,但是一个严重的问题是您没有转义用户的输入以防止像这样的正则表达式元字符.
和*
被解释为这样。为此,您可以使用apos.utils.regExpQuote(s)
,它返回带有通过 . 转义的危险字符的字符串\
。
不过有一个更好的方法可以做到这一点:只需使用req.query.autocomplete
。Apostrophe 有一个内置的autocomplete
游标过滤器,其工作方式与search
过滤器不同。过滤器autocomplete
允许部分匹配(尽管仅在单词的开头),然后它通过常规提供它找到的单词,search
以便结果仍然按匹配质量排序。它还保留了使用search
.
像您这样的正则表达式搜索将扫描整个 mongodb 集合(嗯,至少是相关类型的所有文档),这意味着如果您有很多内容,您将遇到性能问题。
One caveat with autocomplete
is that it only "sees" words in high-priority fields like title, tags, etc. It does not see the full text of a doc the way your regex search (or the search
filter) can. This was a necessary tradeoff to keep the performance up.