gpt4 book ai didi

elasticsearch - Mongoose Elasticsearch 中的地理编码过滤器

转载 作者:行者123 更新时间:2023-12-02 23:36:28 25 4
gpt4 key购买 nike

我正在尝试在meanjs中的mongodb中进行地理点过滤。我使用过 mongoosastic 模块,但我无法执行地理点过滤器。
下面是过滤器的 Mongoose 模式和 Controller 代码。

Mongoose 模式

    'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
mongoosastic = require('mongoosastic');

var BusinessSchema = new Schema({
name: {type: String, unique: 'Name already exists', trim: true, required: 'Name is required.', es_indexed: true},
searchTags: {type: [String], es_indexed: true},
alias: {type: Array, es_indexed: true},
// geoLocation: { type: [Number], /*/ [<longitude>, <latitude>]*/ index: '2d', /*/ create the geospatial index,*/ required: 'GeoLocation is required.', es_indexed:true,es_type:'geo_point'},
geo_cords: {
type: Array
},
address: {
address1: {type: String, required: 'Address is required', trim: true},
address2: String,
city: {type: String, required: 'City is required', trim: true},
// state: {type: String, required: 'State is required', trim: true},
country: {type: String, required: 'Country is required', trim: true},
postalCode: {type: String, required: 'Postal code is required', trim: true},
neighbourhood: String
},
isActive: {
type: Boolean,
default: true,
es_indexed: true
},
dateUpdated: {
type: Date
, es_indexed: true
},
dateCreated: {
type: Date,
default: Date.now
, es_indexed: true

}
});

过滤和查询的 Controller 代码
   var mongoose = require('mongoose'),
Business = mongoose.model('Businesses');

var query = {
"query_string": {
"multi_match": {
"query": categoryIds.join(' OR '),
"fields": ["categoryIds", "relatedCategoryIds"]
}
},
"filter": {
"bool": {
"should": [
{"term": {"address.postalCode": "110016"}},
{"geo_distance": {
"distance": "50km",
"geo_cords": [-122.3050, 37.9174]
}
}
],
}
}
}

Business.search(query, function (err, results) {
// sendResponse(req, res, err, results)
if (!err) {
res.json(results);
} else {
res.status(400).send({message: 'Business Not Found'})
}
});

在这样做时,我收到一个很长的错误说
QueryParsingException[[businessess] failed to find geo_point field [geo_cords]

最佳答案

根据 mongoosastic 的文档

Geo mapping

Prior to index any geo mapped data (or calling the synchronize), the mapping must be manualy created with the createMapping (see above).


首先,在您的架构中,以这种方式定义“geo_cords”:
geo_cords: : {
geo_point: {
type: String,
es_type: 'geo_point',
es_lat_lon: true
},
lat: { type: Number },
lon: { type: Number }
}
将 es_type: 'object' 添加到每个 Array 或嵌入类型
alias: {type: Array, es_indexed: true, es_type: 'object'}
然后在创建模型后立即在模型上调用 .createMapping()。

关于elasticsearch - Mongoose Elasticsearch 中的地理编码过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30500454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com