创建Map时出错{错误:[mapper\u parsing\u exception]字段[category]上声明的类型[string]没有处理程序

fxnxkyjh  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(314)

我不能在这里解决这个问题。我已经尝试创建Map,以便我的代码可以与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);
mspsb9vt

mspsb9vt1#

您必须在模式声明中声明esu类型,如下所示:

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'}
});

相关问题