我正在尝试使用elasticsearch和Tire在Rails 4应用程序中创建搜索页面 . 我想要实现的是,如果用户访问/工作人员,系统会显示带有方面搜索等的工作人员列表页面以及最后添加的20个记录 . 因此,当用户第一次点击页面时,默认情况下,当没有给出查询时,Elasticsearch应该返回20条记录 .

因此,在我对船员的索引行动中,我称之为搜索方法

def index
    @crews = Crew.search(params)
  end

这是我的搜索方法,在没有给出“查询”时返回所有值:

def self.search(params={})

    tire.search(page: params[:page], per_page: 20) do
      query do
        boolean do
          must { string params[:query], default_operator: "AND" } if params[:query].present?
          # just list all if no query given
          must { string "*"} if params[:query].nil?
        end
      end

      filter :term, 'crew_status.name.exact' => params[:crew_status] if params[:crew_status].present?
      sort { by :updated_at, "desc" } if params[:query].blank?

      facet "status" do
        terms :crew_status_id
        terms 'crew_status.name.exact'
      end
      # raise to_curl
    end
  end

这有效,但是当Elasticsearch中没有找到记录时,Tire会给出一个错误“SearchPhaseExecutionException [无法执行阶段[查询]”,这个当前的解决方案也感觉像是一个黑客,我想知道如何以正确的方式完成我的首选行为 . 如果没有给出查询,那只是加载所有记录,如果结果为0则不会出错 .

我以前和Sunspot一起工作过,因为我没有弄错我想让我的工作人员的方式#signal page to work默认为Sunspot . 我希望有人可以帮助我,并指出我正确的方向在Tire / Elasticsearch中正确地做到这一点 .

提前致谢!

招呼,丹尼尔