Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove fulltext index on posts #828

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,13 @@ def index
search_string = []
search_params = {}
[[:username, username, username_operation], [:title, title, title_operation],
[:why, why, why_operation]].each do |si|
[:body, body, body_operation], [:why, why, why_operation]].each do |si|
if si[1].present? && si[1] != '%%'
search_string << "IFNULL(`posts`.`#{si[0]}`, '') #{si[2]} :#{si[0]}"
search_params[si[0]] = si[1]
end
end

if body.present?
if ['LIKE', 'NOT LIKE'].include?(body_operation) && params[:body_is_like] != '1'
# If the operation would be LIKE, hijack it and use our fulltext index for a search instead.
# UNLESS... params[:body_is_like] is set, in which case the user has explicitly specified a LIKE query.
@results = @results.match_search(body, with_search_score: false, posts: :body)
else
if params[:body_is_like] == '1' && !user_signed_in?
flash[:warning] = 'Unregistered users cannot use LIKE searches on the body field. Please sign in.'
redirect_to(search_path) && return
end
# Otherwise, it's REGEX or NOT REGEX, which fulltext won't do - fall back on search_string and params
search_string << "IFNULL(`posts`.`body`, '') #{body_operation} :body"
search_params[:body] = body.present? ? body : '%%'
end
end

@results = @results.where(search_string.join(params[:or_search].present? ? ' OR ' : ' AND '), **search_params)
.paginate(page: params[:page], per_page: per_page)
.order(Arel.sql('`posts`.`created_at` DESC'))
Expand Down
4 changes: 0 additions & 4 deletions app/views/search/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
</div>
<div class="form-group">
<%= label_tag :body %>
<%= check_box_tag :body_is_like, 1, params[:body_is_like] %>
<span class="text-muted">
LIKE (<a href="https://github.com/Charcoal-SE/metasmoke/wiki/FAQ:-search">?</a>)
</span>
<% if current_user&.can_use_regex_search? %>
<%= check_box_tag :body_is_regex, 1, params[:body_is_regex] %> <span class="text-muted">regex</span>
<%= check_box_tag :body_is_inverse_regex, 1, params[:body_is_inverse_regex] %> <span class="text-muted">invert</span>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20201121023305_drop_fulltext_index_of_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DropFulltextIndexOfPosts < ActiveRecord::Migration[5.2]
def change
remove_index :posts, :body
end
end