【问题标题】:error creating mapping { Error: [mapper_parsing_exception] No handler for type [string] declared on field [category]错误创建映射{错误:[mapper_parsing_exception]没有在字段[类别]上声明的类型[字符串]的处理程序
【发布时间】:2019-01-02 21:01:23
【问题描述】:

我无法在这里解决问题。我已经尝试创建映射,以便我的代码可以与 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);

【问题讨论】:

  • 您的代码显示 "category": { "type": "keyword"},而错误显示 type [string] 在字段 [category] 上声明。您确定此代码会产生错误吗?
  • @NishantSaini 是的,此代码正在生成错误,但我无法找出问题所在。你能帮我解决这个问题吗?
  • 尝试使用邮递员直接调用elastic来创建索引。我在该映射中没有发现任何问题。

标签: javascript node.js elasticsearch frontend backend


【解决方案1】:

您必须像这样在架构声明中声明 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'}
});

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多