Add method to Search controller

This will search with trigram first, and then if there aren't enough results, search with prefixed text search
This commit is contained in:
Justin Edmund 2023-07-05 18:46:04 -07:00
parent cca316bb57
commit 5473c1c579

View file

@ -3,6 +3,32 @@
module Api module Api
module V1 module V1
class SearchController < Api::V1::ApiController class SearchController < Api::V1::ApiController
def all
trigram = {
trigram: {
threshold: 0.3
}
}
tsearch = {
tsearch: {
prefix: true,
dictionary: 'simple'
}
}
PgSearch.multisearch_options = { using: trigram }
results = PgSearch.multisearch(search_params[:query]).limit(10)
if (results.length < 5) && (search_params[:query].length >= 2)
PgSearch.multisearch_options = { using: tsearch }
results = PgSearch.multisearch(search_params[:query], { using: tsearch }).limit(10)
end
render json: SearchBlueprint.render(results, root: :results)
end
def characters def characters
filters = search_params[:filters] filters = search_params[:filters]
locale = search_params[:locale] || 'en' locale = search_params[:locale] || 'en'