0

我的架构看起来像这样mongoose

var LocationSchema = new Schema({
    id:          ObjectId,
    geo:         { 
        type: {
            type: String,
            required: true,
            enum: ['Point', 'LineString', 'Polygon'],
            default: 'Point'
        },
        coordinates: [{
            type: Number,
            es_lat_lon: true,
            es_type: 'geo_point'
        }]
    }
});

然后我添加mongoosastic插件来mongoose启动模型并创建映射mongoosastic

var esClient = new elasticsearch.Client({
    host: config.es_url,
    requestTimeout: Infinity,
    keepAlive: true
});
LocationSchema.plugin(mongoosastic, { esClient: esClient })

var Location = mongoose.model('Location', LocationSchema);

/**
 * mongoosastic create mappings
 */
Location.createMapping(function(err, mapping) {
    if (err) {
        console.log('error creating mapping (you can safely ignore this)');
        console.log(err);
    } else {
        console.log('mapping created!');
        console.log(mapping);
    }
});

现在我得到这个错误[Error: MapperParsingException[No handler for type [{default=Point, enum=[Point, LineString, Polygon], required=true, type=string}] declared on field [geo]]]

日志中的完整错误:

{ 
    [Error: MapperParsingException[No handler for type [{default=Point, enum=[Point, LineString, Polygon], required=true, type=string}] declared on field [geo]]]
    status: '400',
    displayName: 'BadRequest',
    message: 'MapperParsingException[No handler for type [{default=Point, enum=[Point, LineString, Polygon], required=true, type=string}] declared on field [geo]]'
}

我的问题是我这样做是完全错误的还是我只是错过了一些小东西?我这样做的方式适用于mongoose,只是mongoosastic遇到了麻烦,我理解为什么,但我不能成为第一个遇到这个问题的人(为什么mongoosastic遇到麻烦是它看到type并且不期望它有一个类型 - 在至少那是我认为它遇到麻烦的地方)。

4

1 回答 1

1

代替

coordinates: [{
        type: Number,
        es_lat_lon: true,
        es_type: 'geo_point'
    }]

尝试

coordinates: {
        type: [Number],
        es_type: 'geo_point'
    }

编辑

OP 发布了这个解决方案。改变:

geo: { 
    type: { 
        type: String, ...... 
    }, 
    coordinates: [
       { type: Number,      ..... }
    ] 
} 

geo: { 
    point_type: { 
        type: String, ...... 
    }, 
    coordinates: [
        { type: Number, ..... }
    ] 
}
于 2016-03-08T15:59:42.963 回答