创建映射时发生错误{错误:[mapper_parsing_exception]在字段[category]上未声明类型[string]的处理程序

时间:2019-01-02 21:01:24

标签: javascript node.js elasticsearch frontend backend

我无法在这里解决问题。我尝试创建映射,以便我的代码可以与ES6x兼容,但仍然无法解决问题。我的代码可能存在一些问题,但我无法查明确切的问题。

这是我的index.js代码

Product.createMapping(
    {

           "products": {
               "properties": {
                   "category": {
                       "type": "keyword"
                   },
                   "name": {
                       "type": "text"
                   },
                   "price": {
                       "type": "double"
                   },
                   "image": {
                       "type": "text"
                   }
               }
           }


    }, function(err, mapping){
    if (err) {
        console.log('error creating mapping');
        console.log(err);
    }

    else{
        console.log('Mapping successfully created');
        console.log(mapping);
    }
});

var data = Product.synchronize();
var count = 0;

data.on('date', function(){
    count++;
});

data.on('close', function(){
    console.log('Indexed' + count + 'Documents');
});

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

这是我的product.js代码。

var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;

var schema = new Schema({
    category: {type: Schema.Types.ObjectId, ref: 'Category'},
    image: {type: String, required: true},
    name: {type: String, required: true},
    price: {type: Number, required: true}
});

schema.plugin(mongoosastic,{
    hosts: [
        'localhost:9200'
    ]
});



module.exports = mongoose.model('Product', schema);

1 个答案:

答案 0 :(得分:0)

您必须像这样在架构声明中声明es_type:

var schema = new Schema({
category: {type: Schema.Types.ObjectId, ref: 'Category' ,es_type:'keyword'},
image: {type: String, required: true ,es_type:'text'},
name: {type: String, required: true ,,es_type:'text'},
price: {type: Number, required: true ,,es_type:'double'}
});