2017-08-22 9 views
1

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 - THESE PASS OF ALL

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 

注:私はコメントアウト3 'キーライン' からaccount.rb、これらのテストのうちの2番目のテストは失敗します。したがって#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"を探して、私は(respecitvely)空の結果を得る:

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): [] 

あなたは問題がどのようなものか考えていますか?

答えて

0

新しいアイテムをインデックス登録/再インデックスするには、しばらくしてAccount.reindexに電話する必要があります。または、after_commitコールバック内のモデルにAccount.reindexを追加することもできます。この方法で、新しいデータは各挿入/コミット後に索引付けされます。

関連する問題