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

How to save polygon data on Elasticsearch through Django GeoShapeField inside NestedField?

模型看起来像 -

    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 字段时,它总是 returns 为空,因为它不是多边形类型数据。

那么,如何通过嵌套的 geoshape 字段通过 django 在 elasticsearch 上保存多边形数据?

索引数据时有类型。而不是 ploygon_zone,它应该是 polygon_zone。我相信修正错字将解决您面临的问题。