У меня странное поведение с оболочкой Elasticsearch Ruby gem Searchkick, и мне нужна ваша помощь!
Проблема
Ожидаемое поведение заключается в том, что я должен иметь возможность искать полное имя с пробелом в нем. Это работает в моем тесте, но не в браузере.
Код
account.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
, второй из этих тестов не пройден. Таким образом, name:
в методе #search_data
, кажется, работает.
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): []
Вы все понимаете, в чем может быть дело?