Skip to content

Commit 142f537

Browse files
authored
Merge pull request #415 from PDOK/remove-bbox-index-and-intersects
fix(features_search): we only need to intersect on the geom, not the bbox
2 parents 6083054 + aefb7c0 commit 142f537

File tree

2 files changed

+5
-23
lines changed

2 files changed

+5
-23
lines changed

internal/ogc/features/datasources/postgres/postgres.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (pg *Postgres) SearchFeaturesAcrossCollections(ctx context.Context, criteri
236236
criteria.OutputSRID = d.WGS84SRIDPostgis
237237
}
238238

239-
bboxFilter, bboxQueryArgs, err := bboxToSQL(criteria.Bbox, criteria.InputSRID, "r."+searchGeomColumn, "r."+searchBboxColumn)
239+
bboxFilter, bboxQueryArgs, err := bboxToSQL(criteria.Bbox, criteria.InputSRID, "r."+searchGeomColumn)
240240
if err != nil {
241241
return nil, err
242242
}
@@ -313,7 +313,7 @@ func (pg *Postgres) makeFeaturesQuery(propConfig *config.FeatureProperties, rela
313313
var bboxNamedParams map[string]any
314314
if criteria.Bbox != nil {
315315
var err error
316-
bboxClause, bboxNamedParams, err = bboxToSQL(criteria.Bbox, criteria.InputSRID, table.GeometryColumnName, "")
316+
bboxClause, bboxNamedParams, err = bboxToSQL(criteria.Bbox, criteria.InputSRID, table.GeometryColumnName)
317317
if err != nil {
318318
return "", nil, err
319319
}
@@ -488,21 +488,14 @@ func makeSearchQuery(index string, bboxFilter string, axisOrder d.AxisOrder) str
488488
LIMIT (@lm::int)`, index, selectGeom, selectBbox, bboxFilter) // don't add user input here, use named params for user input!
489489
}
490490

491-
func bboxToSQL(bbox *geom.Bounds, bboxSRID d.SRID, geomColumn string, bboxColumn string) (string, map[string]any, error) {
491+
func bboxToSQL(bbox *geom.Bounds, bboxSRID d.SRID, geomColumn string) (string, map[string]any, error) {
492492
var bboxFilter, bboxWkt string
493493
var bboxNamedParams map[string]any
494494
var err error
495495
if bbox != nil {
496-
if bboxColumn == "" {
497-
bboxFilter = fmt.Sprintf(`and
496+
bboxFilter = fmt.Sprintf(`and
498497
st_intersects(st_transform(%[1]s, @bboxSrid::int), st_geomfromtext(@bboxWkt::text, @bboxSrid::int))
499498
`, geomColumn)
500-
} else {
501-
bboxFilter = fmt.Sprintf(`and
502-
(st_intersects(st_transform(%[1]s, @bboxSrid::int), st_geomfromtext(@bboxWkt::text, @bboxSrid::int)) or
503-
st_intersects(st_transform(%[2]s, @bboxSrid::int), st_geomfromtext(@bboxWkt::text, @bboxSrid::int)))
504-
`, geomColumn, bboxColumn)
505-
}
506499
bboxWkt, err = wkt.Marshal(bbox.Polygon())
507500
if err != nil {
508501
return "", nil, err

internal/ogc/features_search/etl/load/postgres_ddl.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import (
1212
const (
1313
indexNameFullText = "ts_idx"
1414
indexNameGeometry = "geometry_idx"
15-
indexNameBbox = "bbox_idx"
1615
indexNamePreRank = "pre_rank_idx"
1716
)
1817

1918
var (
2019
postgresExtensions = []string{"postgis", "unaccent", "pg_prewarm", "pg_buffercache"}
2120

22-
indexNames = []string{indexNameFullText, indexNameGeometry, indexNameBbox, indexNamePreRank}
21+
indexNames = []string{indexNameFullText, indexNameGeometry, indexNamePreRank}
2322

2423
//nolint:dupword
2524
tableDefinition = `
@@ -217,16 +216,6 @@ func (p *Postgres) createIndexes(table string, usePrefix bool) error {
217216
return fmt.Errorf("error creating geometry GIST index: %w", err)
218217
}
219218

220-
// GIST indexes for bbox column to support search within a bounding box
221-
indexName = indexNameBbox
222-
if usePrefix {
223-
indexName = fmt.Sprintf("%s_%s", table, indexNameBbox)
224-
}
225-
_, err = p.db.Exec(context.Background(), fmt.Sprintf(`create index if not exists %[2]s on only %[1]s using gist(bbox);`, table, indexName))
226-
if err != nil {
227-
return fmt.Errorf("error creating bbox GIST index: %w", err)
228-
}
229-
230219
// index used to pre-rank results when generic search terms are used
231220
indexName = indexNamePreRank
232221
if usePrefix {

0 commit comments

Comments
 (0)