使用带有 mongoose static 的 Elastic Search Client 进行条件更新

Conditional update with Elastic Search Client with mongoose static

我有一个 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;

您可以使用过滤索引

从 npmjs 复制粘贴

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

对于忽略 Elasticsearch 索引的条件,过滤函数必须 return 为真。

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”类型的 Movie 模型实例不会被索引到 Elasticsearch。

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

你可以这样做

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