New to elasticsearch-rails
. It is acting werid.
When I call my API for the first time, at times, it responds with empty array but calling the same API again, returns proper response.
API Output - For the first time
My model :
class Consultation < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
after_save :set_index
Consultation.import force: true
def set_index
self.__elasticsearch__.index_document
end
end
My controller :
def search
required_params_present = check_required_params(%i[search])
if required_params_present
searched_result = Elasticsearch::Model.search("*#{params[:search]}*", [Consultation]).records.records
data = ActiveModel::ArraySerializer.new(searched_result, each_serializer: ConsultationSerializer)
send_response(HTTP_STATUS_CODE_200, true, I18n.t('search'), data)
else
send_response(HTTP_STATUS_CODE_801, false, I18n.t('params_missing.error'))
end
rescue => e
send_response(HTTP_STATUS_CODE_500, false, e.message)
end
Response is empty only for the first time. Is it that Elasticsearch take times to respond for the first time?
Any help or ideas will be really appreciated?