Ruby on rails Searchkick搜索数据在测试中工作,但不在浏览器中
我在使用Elasticsearch包装器Ruby gem时遇到了一些奇怪的行为,我希望您的帮助 问题 预期的行为是,我应该能够搜索包含空格的全名。这在我的测试中有效,但在浏览器中无效 代码 account.rb 并发症 尽管如此,在浏览器中,它似乎不起作用。用上面的代码,通过浏览器搜索,我无法重现成功 搜索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
“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): []