ElasticSearch 中的 mapper_parsing_exception 错误

Error in mapper_parsing_exception on ElasticSearch

服务器在端口 3000 上启动。

Error creating mapping[mapper_parsing_exception] No handler for type [string] declared on field [category] :: {"path":"/products/_mapping/product","query":{},"body":"{\"product\":{\"properties\":{\"category\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"price\":{\"type\":\"double\"},\"image\":{\"type\":\"string\"}}}}","statusCode":400,"response":"{\"error\":{\"root_cause\":[{\"type\":\"mapper_parsing_exception\",\"reason\":\"No handler for type [string] declared on field [category]\"}],\"type\":\"mapper_parsing_exception\",\"reason\":\"No handler for type [string] declared on field [category]\"},\"status\":400}"}

连接到数据库

索引了 120 个文档

//代码

Product.createMapping(function(err, mapping){
if(err){
    console.log("Error creating mapping"+ err);
}else{
    console.log("Mapping Cretaed");
    console.log(mapping);
}
});

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

stream.on('data', function(){
    count++;
});
stream.on('close', function(){
    console.log("Indexed " + count + "documents");
});
stream.on('error', function(err){
    console.log(err);
});

添加了新代码来解释什么是产品

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

//Schema
var ProductSchema = new Schema({
    category : {
        type : Schema.Types.ObjectId,
        ref : 'Category'
    },
    name : String,
    price : Number,
    image : String 
});
ProductSchema.plugin(mongoosastic, {
    hosts : [
    'localhost:9200'
    ]
})

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

在您尝试创建的映射中,您有一个类型 string,它已在 ES 5.x 中弃用。您需要改用 textkeyword

您的映射应该如下所示:

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

更新:

问题源于截至 2018 年 6 月 26 日,mongoosastic 4.4.1 doesn't support ES5。一种解决方法是像这样修改您的 mongo 架构

category: { 
  type: String,
  es_type: 'keyword'
}
price: { 
  type: Number,
  es_type: 'double'
}
name: { 
  type: String,
  es_type: 'text'
}
image: { 
  type: String,
  es_type: 'text'
}