Allow to create search index without changing unaccent mutability

cf #519
This commit is contained in:
Yohan Boniface 2023-05-10 11:47:29 +02:00
parent 1194bd3b77
commit cf149bc450
2 changed files with 10 additions and 10 deletions

View file

@ -82,15 +82,15 @@ Start the server
UMap uses PostgreSQL tsvector for searching. In case your database is big, you UMap uses PostgreSQL tsvector for searching. In case your database is big, you
may want to add an index. For that, you should do so: may want to add an index. For that, you should do so:
# Create a basic search configuration
CREATE TEXT SEARCH CONFIGURATION umapdict (COPY=simple);
# If you also want to deal with accents and case, add this before creating the index
CREATE EXTENSION unaccent; CREATE EXTENSION unaccent;
CREATE EXTENSION btree_gin; CREATE EXTENSION btree_gin;
ALTER FUNCTION unaccent(text) IMMUTABLE; ALTER TEXT SEARCH CONFIGURATION umapdict ALTER MAPPING FOR hword, hword_part, word WITH unaccent, simple;
ALTER FUNCTION to_tsvector(text) IMMUTABLE;
CREATE INDEX search_idx ON leaflet_storage_map USING gin(to_tsvector(unaccent(name)), share_status);
# Now create the index
CREATE INDEX IF NOT EXISTS search_idx ON umap_map USING GIN(to_tsvector('umapdict', name), share_status);
## Optimisations And change `UMAP_SEARCH_CONFIGURATION = "umapdict"` in your settings.
To speed up uMap homepage rendering on a large instance, the following index can be added as well (make sure you set the center to your default instance map center):
CREATE INDEX leaflet_storage_map_optim ON leaflet_storage_map (modified_at) WHERE ("leaflet_storage_map"."share_status" = 1 AND ST_Distance("leaflet_storage_map"."center", ST_GeomFromEWKT('SRID=4326;POINT(2 51)')) > 1000.0);

View file

@ -179,9 +179,9 @@ class Search(TemplateView, PaginatorMixin):
q = self.request.GET.get("q") q = self.request.GET.get("q")
results = [] results = []
if q: if q:
where = "to_tsvector(name) @@ plainto_tsquery(%s)" where = "to_tsvector(name) @@ websearch_to_tsquery(%s)"
if getattr(settings, "UMAP_USE_UNACCENT", False): if getattr(settings, "UMAP_USE_UNACCENT", False):
where = "to_tsvector(unaccent(name)) @@ plainto_tsquery(unaccent(%s))" # noqa where = "to_tsvector(unaccent(name)) @@ websearch_to_tsquery(unaccent(%s))" # noqa
results = Map.objects.filter(share_status=Map.PUBLIC) results = Map.objects.filter(share_status=Map.PUBLIC)
results = results.extra(where=[where], params=[q]) results = results.extra(where=[where], params=[q])
results = results.order_by("-modified_at") results = results.order_by("-modified_at")