retrospring/app/controllers/search_controller.rb
Georg Gadinger 44871cbf4a add meilisearch
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 :^)
2023-10-22 19:51:19 +02:00

35 lines
981 B
Ruby

class SearchController < ApplicationController
def index
@results = []
@query = params[:q]
return if @query.blank?
@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
)["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
h["hits"].map { |hit| [hit["_rankingScore"], results[hit["id"]]] }
end
.sort_by(&:first)
.reverse
.tap { |results| Rails.logger.debug(results) }
.map(&:last)
end
end