diff --git a/pygeoapi/provider/elasticsearch_.py b/pygeoapi/provider/elasticsearch_.py index fa77117..da8602a 100644 --- a/pygeoapi/provider/elasticsearch_.py +++ b/pygeoapi/provider/elasticsearch_.py @@ -223,25 +223,34 @@ class ElasticsearchProvider(BaseProvider): query['_source']['includes'].append('geometry') try: LOGGER.debug('querying Elasticsearch') - if startindex + limit > 10000: + + LOGGER.debug('Setting ES paging zero-based') + if startindex > 0: + startindex2 = startindex - 1 + else: + startindex2 = startindex + + if startindex2 + limit > 10000: gen = helpers.scan(client=self.es, query=query, preserve_order=True, index=self.index_name) results = {'hits': {'total': limit, 'hits': []}} - for i in range(startindex + limit): + for i in range(startindex2 + limit): try: - if i >= startindex: + if i >= startindex2: results['hits']['hits'].append(next(gen)) else: next(gen) except StopIteration: break results['hits']['total'] = \ - len(results['hits']['hits']) + startindex + len(results['hits']['hits']) + startindex2 else: results = self.es.search(index=self.index_name, - from_=startindex, size=limit, + from_=startindex2, size=limit, body=query) + results['hits']['total'] = results['hits']['total']['value'] + except exceptions.ConnectionError as err: LOGGER.error(err) raise ProviderConnectionError() @@ -252,7 +261,7 @@ class ElasticsearchProvider(BaseProvider): LOGGER.error(err) raise ProviderQueryError() - feature_collection['numberMatched'] = results['hits']['total']['value'] + feature_collection['numberMatched'] = results['hits']['total'] if resulttype == 'hits': return feature_collection diff --git a/tests/cite/ogcapi-features/cite.config.yml b/tests/cite/ogcapi-features/cite.config.yml index 2d937cb..1605d8a 100644 --- a/tests/cite/ogcapi-features/cite.config.yml +++ b/tests/cite/ogcapi-features/cite.config.yml @@ -15,7 +15,7 @@ server: attribution: 'Wikimedia maps | Map data © OpenStreetMap contributors' logging: - level: DEBUG + level: ERROR #logfile: /tmp/pygeoapi.log metadata: diff --git a/tests/test_elasticsearch__provider.py b/tests/test_elasticsearch__provider.py index fa1b6b3..2fc1bd8 100644 --- a/tests/test_elasticsearch__provider.py +++ b/tests/test_elasticsearch__provider.py @@ -61,7 +61,7 @@ def test_query(config): results = p.query(startindex=2, limit=1) assert len(results['features']) == 1 - assert results['features'][0]['id'] == 3042030 + assert results['features'][0]['id'] == 3168070 results = p.query(sortby=[{'property': 'nameascii', 'order': 'A'}]) assert results['features'][0]['properties']['nameascii'] == 'Abidjan'