Search code examples
node.jsmongodbelasticsearchmongoosastic

MongoDB with ElasticSearch mapping with Mongoosastic


I'm trying to index an existing collection to ElasticSearch using the mongoosastic plugin for NodeJs. This my schema:

const callCenterSchema = new mongoose.Schema({
    _owner : { type: mongoose.Schema.Types.ObjectId, ref: 'User', es_type: 'object' },
    ivrs: [{
        name: {
            type: String,
            es_type: 'string'
        },
        ivrType: {
            type: String,
            default: 'mobile',
            enum: ['mobile', 'website'],
            es_type: 'string'
        },
        submenu: {
            type: [ CallCenterSubmenu.schema ],
            es_type: 'nested',
            es_include_in_parent: true
        }
    }]
});

callCenterSchema.plugin(mongoosastic, {
    esClient: require('tusla/lib/db/elastic').elastic,
    populate: [
        { path: '_owner' }
    ]
});

let CallCenter = mongoose.model('CallCenter', callCenterSchema);
CallCenter.synchronize()

CallCenter.createMapping(function(err, mapping) {
  if (err) {
    console.error('Error creating mapping for CallCenters', err.message);
  }
});


module.exports = CallCenter;

My submenu schema is like this:

const callcenterSubmenuSchema = new mongoose.Schema({
    name: String,
    key: String,
    waitTime: {
        type: Number
    },
    waitSuffix: String,
    numberOrLink: String,
    auth: {
        canBeSkipped: String,
        fields: {
            type: Array,
            es_type: 'object'
        },
        verification: String,
        regExp: String
    },
    submenu: [this]
}, { _id: false });

I keep getting this specific error, but couldn't solve it. I appreciate if you guys can help me.

Thanks!

Error creating mapping for CallCenters [mapper_parsing_exception] No handler for type [mixed] declared on field [submenu]


Solution

  • I guess the problem is that line:

     type: [ CallCenterSubmenu.schema ]
    

    In error message it says:

    No handler for type [mixed] declared on field [submenu]
    

    So you are trying to specify the type of submenu field as fixed (or elasticsearch infers it as I am not sure) and as I know there is no type as mixed. So ES fires that exception. You must specify a valid type: https://www.elastic.co/guide/en/elasticsearch/reference/master/mapping-types.html