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,8 +226,12 @@ class Artist < ActiveRecord::Base
end end
def other_names_match(string) def other_names_match(string)
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) where("other_names_index @@ to_tsquery('danbooru', E?)", Artist.normalize_name(string).to_escaped_for_tsquery)
end end
end
def group_name_matches(name) def group_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like stripped_name = normalize_name(name).to_escaped_for_sql_like
@@ -241,9 +245,13 @@ class Artist < ActiveRecord::Base
def any_name_matches(name) def any_name_matches(name)
stripped_name = normalize_name(name).to_escaped_for_sql_like stripped_name = normalize_name(name).to_escaped_for_sql_like
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 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) where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', E?))", stripped_name, name_for_tsquery)
end end
end
def search(params) def search(params)
q = scoped q = scoped

View File

@@ -18,8 +18,12 @@ class Comment < ActiveRecord::Base
end end
def body_matches(query) def body_matches(query)
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") where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split).order("comments.id DESC")
end end
end
def hidden(user) def hidden(user)
where("score < ?", user.comment_threshold) where("score < ?", user.comment_threshold)

View File

@@ -99,8 +99,13 @@ class Dmail < ActiveRecord::Base
end end
def search_message(query) def search_message(query)
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) where("message_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
end end
end
def unread def unread
where("is_read = false and is_deleted = false") where("is_read = false and is_deleted = false")

View File

@@ -17,8 +17,12 @@ class ForumPost < ActiveRecord::Base
module SearchMethods module SearchMethods
def body_matches(body) def body_matches(body)
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) where("forum_posts.text_index @@ plainto_tsquery(E?)", body.to_escaped_for_tsquery)
end end
end
def for_user(user_id) def for_user(user_id)
where("forum_posts.creator_id = ?", user_id) where("forum_posts.creator_id = ?", user_id)

View File

@@ -43,8 +43,12 @@ class ForumTopic < ActiveRecord::Base
module SearchMethods module SearchMethods
def title_matches(title) def title_matches(title)
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) where("text_index @@ plainto_tsquery(E?)", title.to_escaped_for_tsquery_split)
end end
end
def active def active
where("is_deleted = false") where("is_deleted = false")

View File

@@ -21,8 +21,12 @@ class Note < ActiveRecord::Base
end end
def body_matches(query) def body_matches(query)
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) where("body_index @@ plainto_tsquery(E?)", query.to_escaped_for_tsquery_split)
end end
end
def post_tags_match(query) def post_tags_match(query)
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', E?)", query.to_escaped_for_tsquery_split) joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', E?)", query.to_escaped_for_tsquery_split)

View File

@@ -24,8 +24,12 @@ class WikiPage < ActiveRecord::Base
end end
def body_matches(query) def body_matches(query)
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) where("body_index @@ plainto_tsquery(?)", query.to_escaped_for_tsquery_split)
end end
end
def search(params = {}) def search(params = {})
q = scoped q = scoped