微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过 NestedField 内的 Django GeoShapeField 在 Elasticsearch 上保存多边形数据?

如何解决如何通过 NestedField 内的 Django GeoShapeField 在 Elasticsearch 上保存多边形数据?

模型看起来像 -

    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。我相信修正错字将解决您面临的问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。