【问题标题】:Conditional update with Elastic Search Client with mongoose static使用 Elastic Search 客户端和猫鼬静态进行条件更新
【发布时间】:2017-06-01 02:14:00
【问题描述】:

我有一个mongoose 架构,当对此调用保存或更新时,它也会更新弹性搜索源。当status 值为draft 时,我遇到了一个问题,它不应该更新弹性搜索。如何通过在以下模式中进行修改来实现?

var TestShcema = new mongoose.Schema({
        custom_id:{
            type:String,
            required: true,
            index: {unique: true},
            es_indexed: true,
            es_index:"analyzed",
            es_index_analyzer:"autocomplete_analyzer"
        },
        title:{
            type:String,
            index: {unique: false},
            es_indexed: true,
            es_index:"analyzed",
            es_index_analyzer:"autocomplete_analyzer"
        },
        status:{
            type:String,
            index: {unique: false},
            es_indexed: true,
            es_index:"analyzed",
            es_index_analyzer:"autocomplete_analyzer"
        }
    });
    //Hook with Elastic Search
    var esClient = new elasticsearch.Client({host: config.elasticsearch.host});

    TestShcema.plugin(mongoosastic, {
        esClient: esClient
    });

    var Test = mongoose.model('Test', TestShcema);

    module.exports = Test;

【问题讨论】:

    标签: node.js elasticsearch elasticsearch-plugin mongoose-schema mongoose-populate


    【解决方案1】:

    您可以使用过滤索引

    从 npmjs 复制粘贴

    您可以指定过滤函数,根据某些特定条件将模型索引到 Elasticsearch。

    对于忽略 Elasticsearch 索引的条件,过滤函数必须返回 True。

    var MovieSchema = new Schema({
      title: {type: String},
      genre: {type: String, enum: ['horror', 'action', 'adventure', 'other']}
    });
    
    MovieSchema.plugin(mongoosastic, {
      filter: function(doc) {
        return doc.genre === 'action';
      }
    });
    

    类型为“action”的电影模型实例不会被 Elasticsearch 索引。

    https://www.npmjs.com/package/mongoosastic#filtered-indexing

    你可以这样做

    TestShcema.plugin(mongoosastic, {
      filter: function(doc) {
        return doc.status === 'draft';
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多