Mongoosastic 地理空间模式映射器
Mongoosastic geospatial schema mapper
我的架构对于 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
并且不希望它有一个类型 - 至少我认为这是有问题的)。
而不是
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, ..... }
]
}
我的架构对于 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
并且不希望它有一个类型 - 至少我认为这是有问题的)。
而不是
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, ..... }
]
}