mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-02-26 20:53:04 +01:00
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 :^)
35 lines
981 B
Ruby
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
|