Skip to content

Commit e5be8a7

Browse files
authored
Add numberReturned and numberMatched fields to ItemCollection (#254)
1 parent 1ee66a1 commit e5be8a7

File tree

6 files changed

+75
-81
lines changed

6 files changed

+75
-81
lines changed

src/pgstac/sql/004_search.sql

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -893,27 +893,24 @@ BEGIN
893893
RAISE NOTICE 'Time to get prev/next %', age_ms(timer);
894894
timer := clock_timestamp();
895895

896-
IF context(_search->'conf') != 'off' THEN
897-
context := jsonb_strip_nulls(jsonb_build_object(
898-
'limit', _limit,
899-
'matched', total_count,
900-
'returned', coalesce(jsonb_array_length(out_records), 0)
901-
));
902-
ELSE
903-
context := jsonb_strip_nulls(jsonb_build_object(
904-
'limit', _limit,
905-
'returned', coalesce(jsonb_array_length(out_records), 0)
906-
));
907-
END IF;
908-
909896
collection := jsonb_build_object(
910897
'type', 'FeatureCollection',
911898
'features', coalesce(out_records, '[]'::jsonb),
912899
'next', next,
913-
'prev', prev,
914-
'context', context
900+
'prev', prev
915901
);
916902

903+
IF context(_search->'conf') != 'off' THEN
904+
collection := collection || jsonb_strip_nulls(jsonb_build_object(
905+
'numberMatched', total_count,
906+
'numberReturned', coalesce(jsonb_array_length(out_records), 0)
907+
));
908+
ELSE
909+
collection := collection || jsonb_strip_nulls(jsonb_build_object(
910+
'numberReturned', coalesce(jsonb_array_length(out_records), 0)
911+
));
912+
END IF;
913+
917914
IF get_setting_bool('timing', _search->'conf') THEN
918915
collection = collection || jsonb_build_object('timing', age_ms(init_ts));
919916
END IF;
@@ -922,7 +919,7 @@ BEGIN
922919
timer := clock_timestamp();
923920

924921
RAISE NOTICE 'Total Time: %', age_ms(current_timestamp);
925-
RAISE NOTICE 'RETURNING % records. NEXT: %. PREV: %', collection->'context'->>'returned', collection->>'next', collection->>'prev';
922+
RAISE NOTICE 'RETURNING % records. NEXT: %. PREV: %', collection->>'numberReturned', collection->>'next', collection->>'prev';
926923
RETURN collection;
927924
END;
928925
$$ LANGUAGE PLPGSQL;

src/pgstac/sql/004a_collectionsearch.sql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ BEGIN
139139

140140
ret := jsonb_build_object(
141141
'collections', out_records,
142-
'context', jsonb_build_object(
143-
'limit', _limit,
144-
'matched', number_matched,
145-
'returned', number_returned
146-
),
142+
'numberMatched', number_matched,
143+
'numberReturned', number_returned,
147144
'links', links
148145
);
149146
RETURN ret;

src/pgstac/tests/basic/collection_searches.sql.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ SELECT
4040
FROM t1;
4141

4242
select collection_search('{"ids":["testcollection_1","testcollection_2"],"limit":10, "sortby":[{"field":"id","direction":"desc"}]}');
43-
{"links": [], "context": {"limit": 10, "matched": 2, "returned": 2}, "collections": [{"id": "testcollection_2", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-170, -90, -160, -80]]}, "temporal": {"interval": [["2000-01-15 00:00:00+00", "2000-03-15 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_1", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-180, -90, -170, -80]]}, "temporal": {"interval": [["2000-01-08 00:00:00+00", "2000-03-08 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}]}
43+
{"links": [], "collections": [{"id": "testcollection_2", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-170, -90, -160, -80]]}, "temporal": {"interval": [["2000-01-15 00:00:00+00", "2000-03-15 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_1", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-180, -90, -170, -80]]}, "temporal": {"interval": [["2000-01-08 00:00:00+00", "2000-03-08 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}], "numberMatched": 2, "numberReturned": 2}
4444

4545
select collection_search('{"ids":["testcollection_1","testcollection_2"],"limit":10, "sortby":[{"field":"id","direction":"asc"}]}');
46-
{"links": [], "context": {"limit": 10, "matched": 2, "returned": 2}, "collections": [{"id": "testcollection_1", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-180, -90, -170, -80]]}, "temporal": {"interval": [["2000-01-08 00:00:00+00", "2000-03-08 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_2", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-170, -90, -160, -80]]}, "temporal": {"interval": [["2000-01-15 00:00:00+00", "2000-03-15 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}]}
46+
{"links": [], "collections": [{"id": "testcollection_1", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-180, -90, -170, -80]]}, "temporal": {"interval": [["2000-01-08 00:00:00+00", "2000-03-08 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_2", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-170, -90, -160, -80]]}, "temporal": {"interval": [["2000-01-15 00:00:00+00", "2000-03-15 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}], "numberMatched": 2, "numberReturned": 2}
4747

4848
select collection_search('{"ids":["testcollection_1","testcollection_2","testcollection_3"],"limit":1, "sortby":[{"field":"id","direction":"desc"}]}');
49-
{"links": [{"rel": "next", "body": {"offset": 1}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "context": {"limit": 1, "matched": 3, "returned": 1}, "collections": [{"id": "testcollection_3", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-160, -90, -150, -80]]}, "temporal": {"interval": [["2000-01-22 00:00:00+00", "2000-03-22 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}]}
49+
{"links": [{"rel": "next", "body": {"offset": 1}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "collections": [{"id": "testcollection_3", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-160, -90, -150, -80]]}, "temporal": {"interval": [["2000-01-22 00:00:00+00", "2000-03-22 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}], "numberMatched": 3, "numberReturned": 1}
5050

5151
select collection_search('{"ids":["testcollection_1","testcollection_2"],"limit":1, "offset":10, "sortby":[{"field":"datetime","direction":"desc"}]}');
52-
{"links": [{"rel": "prev", "body": {"offset": 9}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}, {"rel": "next", "body": {"offset": 1}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "context": {"limit": 1, "matched": 2, "returned": 0}, "collections": []}
52+
{"links": [{"rel": "prev", "body": {"offset": 9}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}, {"rel": "next", "body": {"offset": 1}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "collections": [], "numberMatched": 2, "numberReturned": 0}
5353

5454
select collection_search('{"filter":{"op":"eq", "args":[{"property":"title"},"My Test Collection."]},"limit":10, "sortby":[{"field":"datetime","direction":"desc"}]}');
55-
{"links": [{"rel": "next", "body": {"offset": 10}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "context": {"limit": 10, "matched": 648, "returned": 10}, "collections": [{"id": "testcollection_648", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[170, 80, 180, 90]]}, "temporal": {"interval": [["2012-06-02 00:00:00+00", "2012-08-02 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_647", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[160, 80, 170, 90]]}, "temporal": {"interval": [["2012-05-26 00:00:00+00", "2012-07-26 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_646", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[150, 80, 160, 90]]}, "temporal": {"interval": [["2012-05-19 00:00:00+00", "2012-07-19 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_645", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[140, 80, 150, 90]]}, "temporal": {"interval": [["2012-05-12 00:00:00+00", "2012-07-12 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_644", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[130, 80, 140, 90]]}, "temporal": {"interval": [["2012-05-05 00:00:00+00", "2012-07-05 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_643", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[120, 80, 130, 90]]}, "temporal": {"interval": [["2012-04-28 00:00:00+00", "2012-06-28 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_642", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[110, 80, 120, 90]]}, "temporal": {"interval": [["2012-04-21 00:00:00+00", "2012-06-21 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_641", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[100, 80, 110, 90]]}, "temporal": {"interval": [["2012-04-14 00:00:00+00", "2012-06-14 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_640", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[90, 80, 100, 90]]}, "temporal": {"interval": [["2012-04-07 00:00:00+00", "2012-06-07 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_639", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[80, 80, 90, 90]]}, "temporal": {"interval": [["2012-03-31 00:00:00+00", "2012-05-31 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}]}
55+
{"links": [{"rel": "next", "body": {"offset": 10}, "href": "./collections", "type": "application/json", "merge": true, "method": "GET"}], "collections": [{"id": "testcollection_648", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[170, 80, 180, 90]]}, "temporal": {"interval": [["2012-06-02 00:00:00+00", "2012-08-02 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_647", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[160, 80, 170, 90]]}, "temporal": {"interval": [["2012-05-26 00:00:00+00", "2012-07-26 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_646", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[150, 80, 160, 90]]}, "temporal": {"interval": [["2012-05-19 00:00:00+00", "2012-07-19 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_645", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[140, 80, 150, 90]]}, "temporal": {"interval": [["2012-05-12 00:00:00+00", "2012-07-12 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_644", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[130, 80, 140, 90]]}, "temporal": {"interval": [["2012-05-05 00:00:00+00", "2012-07-05 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_643", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[120, 80, 130, 90]]}, "temporal": {"interval": [["2012-04-28 00:00:00+00", "2012-06-28 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_642", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[110, 80, 120, 90]]}, "temporal": {"interval": [["2012-04-21 00:00:00+00", "2012-06-21 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_641", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[100, 80, 110, 90]]}, "temporal": {"interval": [["2012-04-14 00:00:00+00", "2012-06-14 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_640", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[90, 80, 100, 90]]}, "temporal": {"interval": [["2012-04-07 00:00:00+00", "2012-06-07 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_639", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[80, 80, 90, 90]]}, "temporal": {"interval": [["2012-03-31 00:00:00+00", "2012-05-31 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}], "numberMatched": 648, "numberReturned": 10}
5656

5757
select collection_search('{"datetime":["2012-01-01","2012-01-02"], "filter":{"op":"eq", "args":[{"property":"title"},"My Test Collection."]},"limit":10, "sortby":[{"field":"datetime","direction":"desc"}]}');
58-
{"links": [], "context": {"limit": 10, "matched": 9, "returned": 9}, "collections": [{"id": "testcollection_626", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-50, 80, -40, 90]]}, "temporal": {"interval": [["2011-12-31 00:00:00+00", "2012-02-29 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_625", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-60, 80, -50, 90]]}, "temporal": {"interval": [["2011-12-24 00:00:00+00", "2012-02-24 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_624", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-70, 80, -60, 90]]}, "temporal": {"interval": [["2011-12-17 00:00:00+00", "2012-02-17 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_623", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-80, 80, -70, 90]]}, "temporal": {"interval": [["2011-12-10 00:00:00+00", "2012-02-10 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_622", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-90, 80, -80, 90]]}, "temporal": {"interval": [["2011-12-03 00:00:00+00", "2012-02-03 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_621", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-100, 80, -90, 90]]}, "temporal": {"interval": [["2011-11-26 00:00:00+00", "2012-01-26 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_620", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-110, 80, -100, 90]]}, "temporal": {"interval": [["2011-11-19 00:00:00+00", "2012-01-19 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_619", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-120, 80, -110, 90]]}, "temporal": {"interval": [["2011-11-12 00:00:00+00", "2012-01-12 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_618", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-130, 80, -120, 90]]}, "temporal": {"interval": [["2011-11-05 00:00:00+00", "2012-01-05 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}]}
58+
{"links": [], "collections": [{"id": "testcollection_626", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-50, 80, -40, 90]]}, "temporal": {"interval": [["2011-12-31 00:00:00+00", "2012-02-29 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_625", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-60, 80, -50, 90]]}, "temporal": {"interval": [["2011-12-24 00:00:00+00", "2012-02-24 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_624", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-70, 80, -60, 90]]}, "temporal": {"interval": [["2011-12-17 00:00:00+00", "2012-02-17 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_623", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-80, 80, -70, 90]]}, "temporal": {"interval": [["2011-12-10 00:00:00+00", "2012-02-10 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_622", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-90, 80, -80, 90]]}, "temporal": {"interval": [["2011-12-03 00:00:00+00", "2012-02-03 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_621", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-100, 80, -90, 90]]}, "temporal": {"interval": [["2011-11-26 00:00:00+00", "2012-01-26 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_620", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-110, 80, -100, 90]]}, "temporal": {"interval": [["2011-11-19 00:00:00+00", "2012-01-19 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_619", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-120, 80, -110, 90]]}, "temporal": {"interval": [["2011-11-12 00:00:00+00", "2012-01-12 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}, {"id": "testcollection_618", "type": "Collection", "title": "My Test Collection.", "extent": {"spatial": {"bbox": [[-130, 80, -120, 90]]}, "temporal": {"interval": [["2011-11-05 00:00:00+00", "2012-01-05 00:00:00+00"]]}}, "description": "Description of my test collection.", "stac_extensions": []}], "numberMatched": 9, "numberReturned": 9}

0 commit comments

Comments
 (0)