-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
this thing is way too fast! only downside is that indexing takes a bit longer, and the search indexes are big (16Gi for 2.7 million records) i have no idea how to properly integrate it in the UI, but it seems promising :^)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,4 @@ gem "mail", "~> 2.7.1" | |
|
||
gem "prometheus-client", "~> 4.2" | ||
|
||
gem "meilisearch-rails", "~> 0.10.1" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
class SearchController < ApplicationController | ||
Check notice on line 1 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L1 <Style/FrozenStringLiteralComment>
Raw output
|
||
def index | ||
@results = [] | ||
query = params[:q] | ||
return if query.blank? | ||
@query = params[:q] | ||
return if @query.blank? | ||
|
||
@results = [] | ||
@results = if params[:multi_search] == "1" | ||
multi_search_experiment | ||
else | ||
[*Answer.search(@query), *Question.search(@query)] | ||
end | ||
end | ||
|
||
private | ||
|
||
def multi_search_experiment | ||
MeiliSearch::Rails.client.multi_search( | ||
[Answer, Question].map do |klass| | ||
{ | ||
q: @query, | ||
index_uid: klass.name.to_s, | ||
show_ranking_score: true, | ||
} | ||
end | ||
Check notice on line 24 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L24 <Style/TrailingCommaInArguments>
Raw output
|
||
)["results"].flat_map do |h| | ||
model = h["indexUid"].constantize # bad practice! | ||
results = model.find(h["hits"].pluck("id")).map { |r| [r.id.to_s, r] }.to_h | ||
Check notice on line 27 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L27 <Rails/IndexBy>
Raw output
Check notice on line 27 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L27 <Style/MapToHash>
Raw output
|
||
h["hits"].map { |hit| [hit["_rankingScore"], results[hit["id"]]] } | ||
end | ||
.sort_by(&:first) | ||
.reverse | ||
.tap { |results| Rails.logger.debug(results) } | ||
Check notice on line 32 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L29-L32 <Style/MultilineBlockChain>
Raw output
|
||
.map(&:last) | ||
Check notice on line 33 in app/controllers/search_controller.rb GitHub Actions / rubocop[rubocop] app/controllers/search_controller.rb#L33 <Layout/MultilineMethodCallIndentation>
Raw output
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
return unless ENV["SEARCH_ENABLED"] == "true" | ||
|
||
MeiliSearch::Rails.configuration = { | ||
meilisearch_url: ENV.fetch("MEILISEARCH_HOST", "http://localhost:7700"), | ||
meilisearch_api_key: ENV.fetch("MEILISEARCH_API_KEY", "justfordev42069e621") | ||
} |