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

时间:2017-01-17 05:59:05

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

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

1 个答案:

答案 0 :(得分: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';
  }
});

具有' 操作'的电影模型实例因为他们的类型不会被索引到Elasticsearch。

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

你可以做这样的事情

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