简体   繁体   English

Elasticsearch-rails:响应有时为空,第二个请求它返回正确的响应

[英]Elasticsearch-rails : response is empty at times, second request it returns proper response

New to elasticsearch-rails . 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时,它以空数组作为响应,但是再次调用相同的API,则返回正确的响应。

API Output - For the first time API输出-首次 在此处输入图片说明

API Output- Second time API输出-第二次 在此处输入图片说明

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? Elasticsearch第一次需要时间才能响应吗?

Any help or ideas will be really appreciated? 任何帮助或想法将不胜感激?

Newly indexed documents are not yet searchable immediately (within ~1 second), for performance reasons. 由于性能原因,新索引的文档尚无法立即(约1秒内)进行搜索。 See reference 参考

You'll need to do a manual "refresh" on the index, if you want realtime search result. 如果需要实时搜索结果,则需要对索引进行手动“刷新”。 However, I quote below 但是,我在下面引用

While a refresh is much lighter than a commit, it still has a performance cost. 尽管刷新比提交轻得多,但仍然会降低性能。 A manual refresh can be useful when writing tests, but don't do a manual refresh every time you index a document in production; 编写测试时,手动刷新可能很有用,但不要在每次为生产中的文档编制索引时都进行手动刷新。 it will hurt your performance. 这会损害您的表现。 Instead, your application needs to be aware of the near real-time nature of Elasticsearch and make allowances for it. 相反,您的应用程序需要了解Elasticsearch的接近实时性质,并为此留出余地。

In test environment, this is perfectly acceptable to do a "refresh" just so you could test immediately that the documents are already searchable. 测试环境中,执行“刷新”是完全可以接受的,只是您可以立即测试文档是否已可搜索。

Because it seems that you are on development , I would advise against this, but you may still free to do so with something like below 因为您似乎正在开发中 ,所以我建议您不要这样做,但是您仍然可以使用下面的方法随意这样做

def set_index
  config = (Rails.env.development? || Rails.env.test?) ? { refresh: true } : {}
  self.__elasticsearch__.index_document config
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM