Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Searchkick搜索数据在测试中工作,但不在浏览器中_Ruby On Rails_Ruby_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Searchkick - Fatal编程技术网 elasticsearch,searchkick,Ruby On Rails,Ruby,elasticsearch,Searchkick" /> elasticsearch,searchkick,Ruby On Rails,Ruby,elasticsearch,Searchkick" />

Ruby on rails Searchkick搜索数据在测试中工作,但不在浏览器中

Ruby on rails Searchkick搜索数据在测试中工作,但不在浏览器中,ruby-on-rails,ruby,elasticsearch,searchkick,Ruby On Rails,Ruby,elasticsearch,Searchkick,我在使用Elasticsearch包装器Ruby gem时遇到了一些奇怪的行为,我希望您的帮助 问题 预期的行为是,我应该能够搜索包含空格的全名。这在我的测试中有效,但在浏览器中无效 代码 account.rb 并发症 尽管如此,在浏览器中,它似乎不起作用。用上面的代码,通过浏览器搜索,我无法重现成功 搜索“bob”时,服务器控制台中的输出为 params[:query]: bob custom_search.map(&:first_name): ["Bob"] custom_searc

我在使用Elasticsearch包装器Ruby gem时遇到了一些奇怪的行为,我希望您的帮助

问题 预期的行为是,我应该能够搜索包含空格的全名。这在我的测试中有效,但在浏览器中无效

代码 account.rb 并发症 尽管如此,在浏览器中,它似乎不起作用。用上面的代码,通过浏览器搜索,我无法重现成功

搜索
“bob”
时,服务器控制台中的输出为

params[:query]: bob
custom_search.map(&:first_name): ["Bob"]
custom_search.map(&:last_name): ["McTesterdorff"]
但只要我搜索
“bob m”
“bob mct”
,或
“bob mctesterdorff”
,我就会得到空结果(分别是):


你们都知道可能会出现什么问题吗?

你们需要偶尔调用
Account.reindex
来索引/重新索引新项目。或者,您也可以在after_commit回调中将
Account.reindex
添加到相应的模型中。这样,每次插入/提交后,新数据都将被索引

class AccountTest < ActiveSupport::TestCase
  describe "::index_search" do
    let(:account_0) do
      FactoryGirl.create(:account, company: "Xyz Consulting")  # name: "Testy McTesterson"
    end
    let(:account_1) do
      FactoryGirl.create(:account, first_name: "Bob") # name: "Bob McTesterson"
    end
    let(:search) do
      Account.index_search(query: query)
    end

    before :all do
      account_0 and account_1 # instantiate
      Searchkick.disable_callbacks
      Account.reindex
    end

    describe "when there are spaces in the string" do
      let(:query) { "xyz con" }

      it "finds all and only the appropriate records" do
        assert_equal [account_0.id], search.map(&:id)
      end
    end

    describe "when the query matches the full name" do
      let(:query) { "bob mct" }

      it "finds all and only the appropriate records" do
        assert_equal [account_1.id], search.map(&:id)
      end
    end
  end
end
class SearchController < ApplicationController
  def index
    puts "params[:query]: #{params[:query]}"
    puts "custom_search.map(&:first_name): #{custom_search.map(&:first_name)}"
    puts "custom_search.map(&:last_name): #{custom_search.map(&:last_name)}"
  end

  private

  def custom_search
   Account.index_search(query: params[:query])
  end
end
params[:query]: bob
custom_search.map(&:first_name): ["Bob"]
custom_search.map(&:last_name): ["McTesterdorff"]
params[:query]: bob m
custom_search.map(&:first_name): []
custom_search.map(&:last_name): []

params[:query]: bob mct
custom_search.map(&:first_name): []
custom_search.map(&:last_name): []

params[:query]: bob mctesterdorff
custom_search.map(&:first_name): []
custom_search.map(&:last_name): []