gpt4 book ai didi

ruby-on-rails - Searchkick 搜索数据在测试中有效但在浏览器中无效

转载 作者:太空宇宙 更新时间:2023-11-03 16:41:57 25 4
gpt4 key购买 nike

我在使用 Elasticsearch 包装器 Ruby gem 时遇到了一些奇怪的行为 Searchkick ,我很乐意得到你的帮助!

问题

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

代码

账户.rb

class Account
searchkick word_start: [
:first_name,
:last_name,
:name, # <======================== key line
:email,
:phone,
:company,
], callbacks: :async

def self.index_search(query:)
search(
query || '*',
fields: [
:first_name,
:last_name,
:name, # <======================== key line
:email,
:phone,
:company,
],
match: :word_start,
where: { test_account: false },
order: { created_at: :desc },
limit: 20,
misspellings: false,
)
end

def search_data
{
first_name: first_name,
last_name: last_name,
name: "#{first_name} #{last_name}", # <======================== key line
created_at: created_at,
email: email,
phone: phone,
test_account: test_account,
company: company
}
end
end

account_test.rb - 所有这些都通过了

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

注意:当我注释掉 account.rb 中的三个“关键行”时,第二个测试失败了。所以 #search_data 方法中的 name: 似乎有效。

search_controller.rb(本质上)

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

并发症

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

当我搜索 "bob " 时,我服务器控制台的输出是

params[:query]: bob
custom_search.map(&:first_name): ["Bob"]
custom_search.map(&:last_name): ["McTesterdorff"]

但是当我搜索 "bob m""bob mct""bob 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): []

你们都知道问题可能是什么吗?

最佳答案

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

关于ruby-on-rails - Searchkick 搜索数据在测试中有效但在浏览器中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45823369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com