当我尝试使用mongosastic弹性搜索到Node应用程序时,我正在使用mapper_parsing_exception

时间:2018-01-16 13:09:53

标签: node.js express elasticsearch mongoose

嘿我正在尝试将弹性搜索应用于我的NodeJS项目,但收到以下错误:

{ Error: [mapper_parsing_exception] No handler for type [string] declared on field [category]

status: 400,
displayName: 'BadRequest',
message: '[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}',
toString: [Function],
toJSON: [Function] }

我的映射代码如下:

product.js 档案:

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

我的 main.js 文件

Product.createMapping(
{
    "mappings": {
        "products": {
            "_all": {
                "analyzer": "nGram_analyzer",
                 "search_analyzer": "whitespace_analyzer"
            }
        }
    }
},function(err, mapping){
if(err){
    console.log('error creating mapping');
    console.log(err);
}else{
    console.log(mapping);
    console.log("mapping created");
}
});

category.js

var CategorySchema = new Schema({
    name: {
        type: String,
        unique: true,
        lowercase: true
    }
 });

我不知道导致此错误的原因。我认为它与ObjectId类型的Mongodb有问题。

2 个答案:

答案 0 :(得分:1)

您似乎尝试将属性指定为string类型。 Elasticsearch不再支持text,而是{&#34}字符串" keywordstring值。

如果您在Elasticsearch的第6版上运行,则在创建映射时会删除{{1}}的向后兼容性:https://www.elastic.co/blog/strings-are-dead-long-live-strings

答案 1 :(得分:0)

Elasticsearch不再支持String,而是为textkeywordString。您可以在https://www.elastic.co/blog/strings-are-dead-long-live-strings上方看到由@ryanlutgen提供的帖子。

要解决此问题,您只需在创建模型时使用es_type即可。由于它会导致类别模型出错,因此您应按如下方式对其进行decalre

var CategorySchema = new Schema({
    name: {
        type: String,
        es_type: 'text' 
        unique: true,
        lowercase: true,
        es_index: true
    }
 });

该解决方案来自https://github.com/mongoosastic/mongoosastic/issues/436