Add support for wildcard searches in text fields

#1663
This commit is contained in:
Toks
2013-07-20 16:51:55 -04:00
parent 0e7ce3397d
commit ecfcebe30a
7 changed files with 42 additions and 9 deletions

View File

@@ -226,7 +226,11 @@ class Artist < ActiveRecord::Base
end
def other_names_match(string)
where("other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
if string =~ /\*/ && CurrentUser.user.is_builder?
where("other_names ILIKE ? ESCAPE E'\\\\'", string.to_escaped_for_sql_like)
else
where("other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
end
end
def group_name_matches(name)
@@ -241,8 +245,12 @@ class Artist < ActiveRecord::Base
def any_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like
name_for_tsquery = normalize_name(name).to_escaped_for_tsquery
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
if name =~ /\*/ && CurrentUser.user.is_builder?
where("(name LIKE ? ESCAPE E'\\\\' OR other_names LIKE ? ESCAPE E'\\\\')", stripped_name, stripped_name)
else
name_for_tsquery = normalize_name(name).to_escaped_for_tsquery
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
end
end
def search(params)

View File

@@ -18,7 +18,11 @@ class Comment < ActiveRecord::Base
end
def body_matches(query)
where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split).order("comments.id DESC")
if query =~ /\*/ && CurrentUser.user.is_builder?
where("body ILIKE ? ESCAPE E'\\\\'", query.to_escaped_for_sql_like)
else
where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split).order("comments.id DESC")
end
end
def hidden(user)

View File

@@ -99,7 +99,12 @@ class Dmail < ActiveRecord::Base
end
def search_message(query)
where("message_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
if query =~ /\*/ && CurrentUser.user.is_builder?
escaped_query = query.to_escaped_for_sql_like
where("(title ILIKE ? ESCAPE E'\\\\' OR body ILIKE ? ESCAPE E'\\\\')", escaped_query, escaped_query)
else
where("message_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
end
end
def unread

View File

@@ -17,7 +17,11 @@ class ForumPost < ActiveRecord::Base
module SearchMethods
def body_matches(body)
where("forum_posts.text_index @@ plainto_tsquery(E?)", body.to_escaped_for_tsquery)
if body =~ /\*/ && CurrentUser.user.is_builder?
where("forum_posts.body ILIKE ? ESCAPE E'\\\\'", body.to_escaped_for_sql_like)
else
where("forum_posts.text_index @@ plainto_tsquery(E?)", body.to_escaped_for_tsquery)
end
end
def for_user(user_id)

View File

@@ -43,7 +43,11 @@ class ForumTopic < ActiveRecord::Base
module SearchMethods
def title_matches(title)
where("text_index @@ plainto_tsquery(E?)", title.to_escaped_for_tsquery_split)
if title =~ /\*/ && CurrentUser.user.is_builder?
where("title ILIKE ? ESCAPE E'\\\\'", title.to_escaped_for_sql_like)
else
where("text_index @@ plainto_tsquery(E?)", title.to_escaped_for_tsquery_split)
end
end
def active

View File

@@ -21,7 +21,11 @@ class Note < ActiveRecord::Base
end
def body_matches(query)
where("body_index @@ plainto_tsquery(E?)", query.to_escaped_for_tsquery_split)
if query =~ /\*/ && CurrentUser.user.is_builder?
where("body ILIKE ? ESCAPE E'\\\\'", query.to_escaped_for_sql_like)
else
where("body_index @@ plainto_tsquery(E?)", query.to_escaped_for_tsquery_split)
end
end
def post_tags_match(query)

View File

@@ -24,7 +24,11 @@ class WikiPage < ActiveRecord::Base
end
def body_matches(query)
where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
if query =~ /\*/ && CurrentUser.user.is_builder?
where("body ILIKE ? ESCAPE E'\\\\'", query.to_escaped_for_sql_like)
else
where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
end
end
def search(params = {})