使用 searchkick 进行多字段搜索
Multi-field search with searchkick
我使用 searchkick 字段搜索 multi-able 的代码似乎不起作用
search = params[:term].present? ? params[:term] : nil
combos = if search
Combo.search(search, fields: [:game_name, :character_name])
else
Combo.all.page(params[:page])
end
我得到一个
F, [2018-05-11T05:11:29.348920 #4] FATAL -- : [2879ef07-9eae-43e2-9c89-9e7a55aac2ae] NoMethodError (#未定义方法`joins'):
在我的 heroku 日志中。我觉得这是一个快速修复,但我很难过。
编辑添加的日志和 gemfile
2018-05-11T06:09:08.074664+00:00 heroku[router]: at=info method=GET path="/" host=combobook.net request_id=6ef470c5-6df6-46ca-b73c-8e6e1bf9a6e3 fwd="72.83.52.163,172.68.65.219" dyno=web.1 connect=0ms service=36ms status=200 bytes=6168 protocol=https
2018-05-11T06:09:55.502333+00:00 heroku[router]: at=info method=GET path="/combos?utf8=%E2%9C%93&term=r&button=" host=combobook.net request_id=107260a0-8790-4e4e-b435-ec7ee557b5ed fwd="72.83.52.163,172.68.65.217" dyno=web.1 connect=1ms service=59ms status=500 bytes=1827 protocol=https
2018-05-11T06:09:55.443112+00:00 app[web.1]: I, [2018-05-11T06:09:55.442916 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Started GET "/combos?utf8=%E2%9C%93&term=r&button=" for 172.68.65.217 at 2018-05-11 06:09:55 +0000
2018-05-11T06:09:55.445117+00:00 app[web.1]: I, [2018-05-11T06:09:55.445002 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Processing by CombosController#index as HTML
2018-05-11T06:09:55.445293+00:00 app[web.1]: I, [2018-05-11T06:09:55.445162 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Parameters: {"utf8"=>"✓", "term"=>"r", "button"=>""}
2018-05-11T06:09:55.488673+00:00 app[web.1]: D, [2018-05-11T06:09:55.488519 #4] DEBUG -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] [1m[33mCombo Search (40.2ms)[0m curl https://spruce-6383279.us-east-1.bonsaisearch.net:443/combos_production/_search?pretty -H 'Content-Type: application/json' -d '{"size":10000,"from":0,"query":{"dis_max":{"queries":[{"match":{"game_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"game_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"game_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"game_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"character_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"character_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"character_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"character_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"timeout":"11s","_source":false}'
2018-05-11T06:09:55.497046+00:00 app[web.1]: I, [2018-05-11T06:09:55.496903 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Completed 500 Internal Server Error in 51ms (Searchkick: 40.2ms | ActiveRecord: 0.0ms)
2018-05-11T06:09:55.498165+00:00 app[web.1]: F, [2018-05-11T06:09:55.498095 #4] FATAL -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed]
2018-05-11T06:09:55.498556+00:00 app[web.1]: F, [2018-05-11T06:09:55.498491 #4] FATAL -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] NoMethodError (undefined method `joins' for #<Searchkick::Results:0x00000005165940>):
gem 'searchkick'
gem 'cocoon'
gem 'rails_admin'
gem 'kaminari'
gem 'figaro'
gem 'sendgrid-ruby'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
gem 'rspec-rails'
end
group :development do
gem 'seed_dump'
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem "rails-erd"
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
添加了相关控制器信息
def sort_and_paginate_combos(combos)
combos.joins(:stars)
.group(:id)
.order('COUNT(stars.id) DESC')
.page(params[:page])
end
嘿,因为我可以检查搜索词是否不存在,它会获取 Active Record 结果而不是 Serachkiq 结果。
您可以尝试以下代码
search = params[:term].present? ? params[:term] : '*'
combos = Combo.search(search, fields: [:game_name, :character_name], page: params[:page])
希望它能工作更多细节请提供有关 combos
迭代的详细信息。
我使用 searchkick 字段搜索 multi-able 的代码似乎不起作用
search = params[:term].present? ? params[:term] : nil
combos = if search
Combo.search(search, fields: [:game_name, :character_name])
else
Combo.all.page(params[:page])
end
我得到一个
F, [2018-05-11T05:11:29.348920 #4] FATAL -- : [2879ef07-9eae-43e2-9c89-9e7a55aac2ae] NoMethodError (#未定义方法`joins'):
在我的 heroku 日志中。我觉得这是一个快速修复,但我很难过。
编辑添加的日志和 gemfile
2018-05-11T06:09:08.074664+00:00 heroku[router]: at=info method=GET path="/" host=combobook.net request_id=6ef470c5-6df6-46ca-b73c-8e6e1bf9a6e3 fwd="72.83.52.163,172.68.65.219" dyno=web.1 connect=0ms service=36ms status=200 bytes=6168 protocol=https
2018-05-11T06:09:55.502333+00:00 heroku[router]: at=info method=GET path="/combos?utf8=%E2%9C%93&term=r&button=" host=combobook.net request_id=107260a0-8790-4e4e-b435-ec7ee557b5ed fwd="72.83.52.163,172.68.65.217" dyno=web.1 connect=1ms service=59ms status=500 bytes=1827 protocol=https
2018-05-11T06:09:55.443112+00:00 app[web.1]: I, [2018-05-11T06:09:55.442916 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Started GET "/combos?utf8=%E2%9C%93&term=r&button=" for 172.68.65.217 at 2018-05-11 06:09:55 +0000
2018-05-11T06:09:55.445117+00:00 app[web.1]: I, [2018-05-11T06:09:55.445002 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Processing by CombosController#index as HTML
2018-05-11T06:09:55.445293+00:00 app[web.1]: I, [2018-05-11T06:09:55.445162 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Parameters: {"utf8"=>"✓", "term"=>"r", "button"=>""}
2018-05-11T06:09:55.488673+00:00 app[web.1]: D, [2018-05-11T06:09:55.488519 #4] DEBUG -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] [1m[33mCombo Search (40.2ms)[0m curl https://spruce-6383279.us-east-1.bonsaisearch.net:443/combos_production/_search?pretty -H 'Content-Type: application/json' -d '{"size":10000,"from":0,"query":{"dis_max":{"queries":[{"match":{"game_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"game_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"game_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"game_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"character_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search"}}},{"match":{"character_name.analyzed":{"query":"r","boost":10,"operator":"and","analyzer":"searchkick_search2"}}},{"match":{"character_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"character_name.analyzed":{"query":"r","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"timeout":"11s","_source":false}'
2018-05-11T06:09:55.497046+00:00 app[web.1]: I, [2018-05-11T06:09:55.496903 #4] INFO -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] Completed 500 Internal Server Error in 51ms (Searchkick: 40.2ms | ActiveRecord: 0.0ms)
2018-05-11T06:09:55.498165+00:00 app[web.1]: F, [2018-05-11T06:09:55.498095 #4] FATAL -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed]
2018-05-11T06:09:55.498556+00:00 app[web.1]: F, [2018-05-11T06:09:55.498491 #4] FATAL -- : [107260a0-8790-4e4e-b435-ec7ee557b5ed] NoMethodError (undefined method `joins' for #<Searchkick::Results:0x00000005165940>):
gem 'searchkick'
gem 'cocoon'
gem 'rails_admin'
gem 'kaminari'
gem 'figaro'
gem 'sendgrid-ruby'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
gem 'rspec-rails'
end
group :development do
gem 'seed_dump'
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem "rails-erd"
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
添加了相关控制器信息
def sort_and_paginate_combos(combos)
combos.joins(:stars)
.group(:id)
.order('COUNT(stars.id) DESC')
.page(params[:page])
end
嘿,因为我可以检查搜索词是否不存在,它会获取 Active Record 结果而不是 Serachkiq 结果。
您可以尝试以下代码
search = params[:term].present? ? params[:term] : '*'
combos = Combo.search(search, fields: [:game_name, :character_name], page: params[:page])
希望它能工作更多细节请提供有关 combos
迭代的详细信息。