来自巨大数据的流星角自动完成

时间:2017-08-26 13:42:12

标签: meteor angular-meteor

我的angular-meteor应用程序需要使用带有angularUtils.directives.dirPagination的53,296个文档的集合中的材料md-autocomplete,但这些数据会使我的浏览器挂起。

我发布了以下集合:

Meteor.publish('city', function (options, searchString) {
  var where = {
    'city_name': {
      '$regex': '.*' + (searchString || '') + '.*' ,
      '$options': 'i'
    }
  };
  return City.find(where, options);
});

我订阅:

subscriptions: function () {
  Meteor.subscribe('city');
  this.register('city', Meteor.subscribe('city'));
}

并在控制器上加入分页:

$scope.currentPage = 1;
$scope.pageSize = 100;
$scope.sort = {city_name_sort : 1};
$scope.orderProperty = '1';
$scope.helpers({
  city: function(){
    return City.find({});
  }
});

但加载时需要很长时间才能使镀铬停止工作。

1 个答案:

答案 0 :(得分:0)

您已完成大部分服务器端搜索,因为您的搜索正在订阅中运行。您应该确保{mno中的city_name字段已编入索引!您应该只返回该字段以最小化数据传输。您还可以简化正则表达式。

Meteor.publish('city', function (searchString) {
  const re = new RegExp(searchString,'i');
  const where = { city_name: { $regex: re }};
  return City.find(where, {sort: {city_name: 1}, fields: {city_name: 1}});
});

我发现服务器端自动完成的帮助是:

  1. 在用户输入3或4个字符之前,请勿开始搜索。这大大缩小了搜索结果。
  2. 将搜索限制为仅每500毫秒运行一次,这样您就不会将每个字符都发送到服务器,因为它必须继续重新执行搜索。如果此人正在快速键入,则搜索可能只会每2或3个字符运行一次。
  3. 在您在服务器上运行的客户端上运行相同的.find()(而不是仅查询{})。这只是一个很好的做法,因为客户端集合是该集合上所有订阅的联合,可能存在您不想列出的文档。
  4. 最后我不知道你为什么要在这里订阅两次:

    subscriptions: function () {
      Meteor.subscribe('city');
      this.register('city', Meteor.subscribe('city'));
    }
    

    只需要其中一个Meteor.subscribe('city')次来电。

相关问题