0

我正在使用 mongoosastic 将文档索引到 ElasticSearch 中。save()、remove() 等工作正常,但是当我 update() 一个文档时,它没有在 elasticsearch 中重新索引。您能否帮助我了解如何更新 mongodb 文档,以便 mongoosastic 自动重新索引更新的文档?非常感谢。

这是代码片段

Product.update({"_id" : {$in : productIDs}}, {l2Category : req.body.newL2CategoryName}, {multi : true}, function(errUpdatingProducts){
        if(errUpdatingProducts)
        console.log(errUpdatingProducts);
});   

在 Product Schema 中,l2Category 被索引为 String

l2Category: {
    type: String,
    es_indexed: true,
    es_type: 'string'
},

一种方法是使用 find() 从 mongodb 服务器将数据加载到客户端,并在客户端更新每个文档,然后使用 save() 将其保存回服务器。但这似乎不是最聪明的方法。

4

1 回答 1

0

像这样创建映射:

ProductSchema.plugin(mongoosastic);
Product = module.exports = mongoose.model('Product', ProductSchema);
Product.createMapping({ 
     "settings": {
     "number_of_shards": 1,
     "number_of_replicas": 0,
     "analysis": {
         "analyzer": {
             "autocomplete": {
                 "type": "custom",
                 "tokenizer": "standard",
                 "filter": ["standard", "lowercase", "stop", "kstem", "ngram"]
             }
         },
         "filter": {
             "ngram": {
                 "type": "ngram",
                 "min_gram": 2,
                 "max_gram": 15
             }
         }
     }
 },
 "mappings": {
     "Product": {
         "properties": {
              "yourschemaobject":...

         }
     }
 }
}, function(err, mapping) {
    if (err) {
        console.log('error creating mapping (you can safely ignore this)');
        console.log(err);
    } else {
        console.log('mapping created!');
        console.log(mapping);
    }
});

在您的映射中创建对象,如我的示例。http://jsfiddle.net/kevalbhatt18/mt652L3m/1/

完成映射后,请尝试使用此示例进行保存和更新https://stackoverflow.com/a/33992421/4696809

于 2015-12-03T11:52:33.043 回答