【问题标题】:meteor full text search. in client after subscribe, collection contain old search results流星全文搜索。在客户端订阅后,集合包含旧的搜索结果
【发布时间】:2017-09-08 10:40:13
【问题描述】:

我正在尝试使用 angular 2 在流星中进行全文搜索。有我的发布功能:

Meteor.publish("search", (searchValue) => {
    console.log(searchValue);
    if (searchValue) {
        return Nutrition.find(
            {$text: {$search: searchValue}},
            {
                // `fields` is where we can add MongoDB projections. Here we're causing
                // each document published to include a property named `score`, which
                // contains the document's search rank, a numerical value, with more
                // relevant documents having a higher score.
                fields: {
                    'name.long': 1,
                    score: {$meta: "textScore"}
                },
                // This indicates that we wish the publication to be sorted by the
                // `score` property specified in the projection fields above.
                sort: {
                    score: {$meta: "textScore"},
                },
                limit: 20
            }
        );
    } else {
        return Nutrition.find({})
    }
});

在客户端:

  public searchProducts = _.debounce((query) => {
        Meteor.subscribe('search', query);
        Nutrition.find({}).subscribe(data=>{
            console.log(data.length);
        });
    }, 500);

但在每次订阅后集合包含新值(来自实际搜索)和旧值(来自旧搜索)。

这可能是什么原因?我能做些什么来避免这种情况?

【问题讨论】:

    标签: angular meteor


    【解决方案1】:
        Nutrition.find({}).subscribe(data=>{
            console.log(data.length);
        });
    

    这很糟糕:删除它。在 Blaze 助手中调用 Nutrition.find({}) 或阅读使用 Meteor 的 Angular/React 集成绑定到数据的适当方法。

    要解决您的具体问题,您需要取消旧订阅。

    var sub = null;
    public searchProducts = _.debounce((query) => {
        if (!!sub) {
            sub.stop();
        }
        sub = Meteor.subscribe('search', query);
    }
    

    这将导致闪烁行为,这是一个不同的问题。您应该扩展您的 Nutrition.find({}) 以过滤查询的某些部分。

    在生产设置中,我实际上使用服务器端的发布句柄创建了一个合成字段,该句柄指示哪些字段满足哪个查询。根据您的需要,这可能是过度设计的(难以正确实施)。

    我推荐https://atmospherejs.com/easy/search 包。

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多