【发布时间】: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