新到elasticsearch-rails
. 它表现得很奇怪。当我第一次调用我的 API 时,有时它会以空数组响应,但再次调用相同的 API,会返回正确的响应。
我的模型:
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
我的控制器:
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
响应仅第一次为空。是不是 Elasticsearch 第一次响应需要时间?
任何帮助或想法将不胜感激?