Search code examples
javascriptnode.jsmongoosemongoosastic

Monsgoosastic es_indexed not working


I have a model schema a part of which I want to index in ElasticSearch. According to the docs, setting:

es_indexed: true

Along with the desired schema field will only index that key on operations related to ElasticSearch. I have a large schema of around 20 fields of which only 5 need to be indexed.

Problem is, these flags are being ignored and the entire document is being indexed.

var PersonSchema = new Mongoose.Schema({
    name: {type: String, es_indexed: true},
    address: address: {
        street_address: {type: String},
        locality: {type: String},
        region: {type: String},
        zip: {type: String},
        landmark: {type: String},
        neighbourhood : {type: [String]}
    },
    ...
    tags: {type:[String], index:true, es_indexed: true},
    ...
})

PersonSchema.plugin(mongoosastic);

var Person = Mongoose.model("Merchant", PersonSchema);

After I call

var stream = Merchant.synchronize()
        , count = 0;

    stream.on('data', function(err, doc){
        count++;
        console.log('indexing: '+ count+ ' done');
    });
    stream.on('close', function(){
        console.log('indexed ' + count + ' documents!');
    });

    stream.on('error', function(err){
        console.log(err);
    });

The entire document is saved, including address and other unnecessary fields. Why are the es_indexed: true flags not working?


Solution

  • Apparently, you have to provide an es_indexed property for all fields, to explicitly say whether you want to include the field or not.

    var PersonSchema = new Mongoose.Schema({
        name: {type: String, es_indexed: true},
        address: address: {
            es_indexed: false,             //Exclusion needs to be specified as well
            street_address: {type: String},
            locality: {type: String},
            region: {type: String},
            zip: {type: String},
            landmark: {type: String},
            neighbourhood : {type: [String]}
        },
        ...
        tags: {type:[String], index:true, es_indexed: true},
        ...
    })