elasticsearchnestelasticsearch-geo-shape

Reading geo_shape as GeoJSON text


I currently have an elasticsearch query through NEST that is retrieving an object such as

    public class ElasticSearchObject
    {
        [PropertyName("code")]
        public string Code { get; set; }

        [PropertyName("geometria")]
        public MultiPolygonGeoShape Geometria { get; set; }
    }

The query right now is working, but I need to change the type for "Geometria" property (which is mapped to a geo_shape field in the elasticseach stored document). Now it has to be a string holding a GeoJSON string. I could post-process the MultiPolygonGeoShape property and build the GeoJSON string from its content, but I wonder... Is it there a more direct approach?

As far as I know the geo_shape field is already stored in the elasticsearch JSON document as a GeoJSON string, so it looks to me like a waste of resources going through reading the geo_shape fromt the JSON document, deserializing into MultiPolygonGeoShape and then postprocessing to GeoJSON, when the GeoJSON was already there from the beginning.


Solution

  • As far as I know the geo_shape field is already stored in the elasticsearch JSON document as a GeoJSON string, so it looks to me like a waste of resources going through reading the geo_shape fromt the JSON document, deserializing into MultiPolygonGeoShape and then postprocessing to GeoJSON, when the GeoJSON was already there from the beginning.

    It's not stored in _source as a GeoJSON string; it's a GeoJSON object with properties such as "type", "coordinates", etc.

    It depends what you want to do with the data as to whether it's a waste of resources, to deserialize to a type. If you're only going to serialize back to GeoJSON again, then it may be overhead to go through the deserialization/serialization roundtrip. In this case, you might be better using the low level client exposed on NEST through the client.LowLevel property to return the complete response as a string or byte array, and deserialize/parse only the properties you're interested in.