geopython / pygeoapi

pygeoapi is a Python server implementation of the OGC API suite of standards. The project emerged as part of the next generation OGC API efforts in 2018 and provides the capability for organizations to deploy a RESTful OGC API endpoint using OpenAPI, GeoJSON, and HTML. pygeoapi is open source and released under an MIT license.
https://pygeoapi.io
MIT License
459 stars 250 forks source link

Error unable to find 'properties' key with ElasticSearch provider #1684

Closed ccancellieri closed 2 weeks ago

ccancellieri commented 2 weeks ago

Hi I'm having problems with the from the ElasticSearch provider not finding the 'properties' key (nested two times, see link below) in the source data dict.

https://github.com/geopython/pygeoapi/blob/f978d0a1f6d9628da24348975747d9e0e21c4ef9/pygeoapi/provider/elasticsearch_.py#L124C17-L124C80

The source data from ElasticSearch has been loaded using Kibana and consists in the attached geojson.json

The code which is now working is below, but I'm wondering why if it was intentional and if I'm doing something wrong:

try:
            if '*' not in self.index_name:
                p = ii[self.index_name]['mappings']
            else:
                LOGGER.debug('Wildcard index; setting from first match')
                index_name_ = list(ii.keys())[0]
                p = ii[index_name_]['mappings']
        except KeyError:
            LOGGER.warning('Trying for alias')
            alias_name = next(iter(ii))
            p = ii[alias_name]['mappings']
        except IndexError:
            LOGGER.warning('could not get fields; returning empty set')
            return {}

        for k, v in p['properties'].items():
            if 'type' in v:
                if v['type'] == 'text':
                    fields_[k] = {'type': 'string'}
                elif v['type'] == 'date':
                    fields_[k] = {'type': 'string', 'format': 'date'}
                elif v['type'] in ('float', 'long'):
                    fields_[k] = {'type': 'number', 'format': v['type']}
                else:
                    fields_[k] = {'type': v['type']}

        return fields_
tomkralidis commented 2 weeks ago

The code in https://github.com/geopython/pygeoapi/blob/f978d0a1f6d9628da24348975747d9e0e21c4ef9/pygeoapi/provider/elasticsearch_.py#L124 is "double nesting" properties of the mappings, not the actual GeoJSON data.

The test data attached in this is of a feature collection, not a feature. From the docs:

To publish an Elasticsearch index, the following are required in your index:

indexes must be documents of valid GeoJSON Features

index mappings must define the GeoJSON geometry as a geo_shape