From 7c6ef9f22c9d3ce86f317838307f801d5a05d9c2 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Thu, 13 Sep 2018 10:52:04 -0700 Subject: [PATCH] Revert "Revert "add prefix index on tags (#3858)"" This reverts commit 6210c1554c8ae97625f205dc0e78c13e92624aeb. --- app/logical/tag_autocomplete.rb | 4 ++-- db/structure.sql | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/logical/tag_autocomplete.rb b/app/logical/tag_autocomplete.rb index d29abe0c1..1238ff3b9 100644 --- a/app/logical/tag_autocomplete.rb +++ b/app/logical/tag_autocomplete.rb @@ -74,9 +74,9 @@ module TagAutocomplete n += 2 end - anchors = "^" + query.split("").map {|x| Regexp.escape(x)}.join(".*[#{PREFIX_BOUNDARIES}]") + regexp = "([a-z0-9])[a-z0-9']*($|[^a-z0-9']+)" Tag - .where("name ~ ?", anchors) + .where('regexp_replace(name, ?, ?, ?) like ?', regexp, '\1', 'g', query.to_escaped_for_sql_like + '%') .where("post_count > ?", min_post_count) .where("post_count > 0") .order("post_count desc") diff --git a/db/structure.sql b/db/structure.sql index 01a7a9368..243ae4d47 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -7128,6 +7128,13 @@ CREATE UNIQUE INDEX index_tags_on_name ON public.tags USING btree (name); CREATE INDEX index_tags_on_name_pattern ON public.tags USING btree (name text_pattern_ops); +-- +-- Name: index_tags_on_name_prefix; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_tags_on_name_prefix ON public.tags USING gin (regexp_replace((name)::text, '([a-z0-9])[a-z0-9'']*($|[^a-z0-9'']+)'::text, ''::text, 'g'::text) public.gin_trgm_ops) WHERE (post_count > 0); + + -- -- Name: index_tags_on_name_trgm; Type: INDEX; Schema: public; Owner: - -- @@ -7524,6 +7531,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20180517190048'), ('20180518175154'), ('20180804203201'), -('20180816230604'); +('20180816230604'), +('20180912185624');