Merge pull request #3894 from r888888888/index-tag-prefix-search
Index tag prefix search
This commit is contained in:
@@ -39,10 +39,6 @@ module TagAutocomplete
|
|||||||
end
|
end
|
||||||
|
|
||||||
def search_correct(query, n=2)
|
def search_correct(query, n=2)
|
||||||
if CurrentUser.id != 1
|
|
||||||
return []
|
|
||||||
end
|
|
||||||
|
|
||||||
if query.size <= 3
|
if query.size <= 3
|
||||||
return []
|
return []
|
||||||
end
|
end
|
||||||
@@ -78,9 +74,9 @@ module TagAutocomplete
|
|||||||
n += 2
|
n += 2
|
||||||
end
|
end
|
||||||
|
|
||||||
anchors = "^" + query.split("").map {|x| Regexp.escape(x)}.join(".*[#{PREFIX_BOUNDARIES}]")
|
regexp = "([a-z0-9])[a-z0-9']*($|[^a-z0-9']+)"
|
||||||
Tag
|
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 > ?", min_post_count)
|
||||||
.where("post_count > 0")
|
.where("post_count > 0")
|
||||||
.order("post_count desc")
|
.order("post_count desc")
|
||||||
|
|||||||
5
db/migrate/20180912185624_add_prefixes_to_tags.rb
Normal file
5
db/migrate/20180912185624_add_prefixes_to_tags.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class AddPrefixesToTags < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
execute "CREATE INDEX index_tags_on_name_prefix ON tags USING gin (REGEXP_REPLACE(name, '([a-z0-9])[a-z0-9'']*($|[^a-z0-9'']+)', '\1', 'g') gin_trgm_ops) WHERE post_count > 0"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -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);
|
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: -
|
-- Name: index_tags_on_name_trgm; Type: INDEX; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
@@ -7524,6 +7531,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||||||
('20180517190048'),
|
('20180517190048'),
|
||||||
('20180518175154'),
|
('20180518175154'),
|
||||||
('20180804203201'),
|
('20180804203201'),
|
||||||
('20180816230604');
|
('20180816230604'),
|
||||||
|
('20180912185624');
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user