问题描述
模型看起来像 -
class Restaurant(models.Model):
zones = JSONField(default=dict)
文档看起来像-
@registry.register_document
class RestaurantDocument(Document):
zone = fields.nestedField(properties={"slug": fields.KeywordField(),"polygon_zone": fields.GeoShapeField()})
class Index:
name = 'restaurant_data'
settings = {
'number_of_shards': 1,'number_of_replicas': 0
}
class Django:
model = Restaurant
def prepare_zone(self,instance):
return instance.zone
索引映射后看起来像-
"zone": {
"type": "nested","properties": {
"polygon_zone": {
"type": "geo_shape"
},"slug": {
"type": "keyword"
}
}
}
但是当我通过以下结构在 zones 字段上保存数据时-
[{"slug":"dhaka","ploygon_zone":{"type":"polygon","coordinates":[[[89.84207153320312,24.02827811169503],[89.78233337402344,23.93040645231774],[89.82833862304688,23.78722976367578],[90.02197265625,23.801051951752406],[90.11329650878905,23.872024546162947],[90.11672973632812,24.00883517846163],[89.84207153320312,24.02827811169503]]]}}]
那么elasticsearch的映射就通过以下方式自动改变了-
"zone": {
"type": "nested","properties": {
"ploygon_zone": {
"properties": {
"coordinates": {
"type": "float"
},"type": {
"type": "text","fields": {
"keyword": {
"type": "keyword","ignore_above": 256
}
}
}
}
},"polygon_zone": {
"type": "geo_shape"
},"slug": {
"type": "keyword"
}
}
}
这就是为什么当我尝试搜索 zone__polygon_zone 字段时,它总是返回空,因为它不是多边形类型的数据。
那么,如何通过嵌套的 geoshape 字段在 elasticsearch 槽 django 上保存多边形数据?
解决方法
索引数据时有一个类型。而不是 ploygon_zone
,它应该是 polygon_zone
。我相信修正错字将解决您面临的问题。