0

I'm trying to run a query to find all items in a given location. I have an Item.rb & Location.rb models where searchkick is present. In my items controller I have the following logic:

if params[:item_name].present?
    @items = Item.search(params[:query], index_name: [Item.searchkick_index.item_name, Location.searchkick_index.city])

(:item_name is used to catch the paramaters)

Ant the following is the log:

Parameters: {"utf8"=>"✓", "item_name"=>"test", "city"=>"Memphis", "commit"=>"Search"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 12]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 12]]
Completed 500 Internal Server Error in 3ms

NoMethodError (undefined method `item_name' for #<Searchkick::Index:0x007fb43858d4e8>):
app/controllers/items_controller.rb:9:in `index'

Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (12.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (5.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (1.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (51.0ms)

I've tried re-indexing Location and Item, restarting the server, to no avail.

How does one build search over two/n models ?

1 Answers1

1

I believe you need to specify the names of your indices only, e.g.

if params[:item_name].present?
    @items = Item.search(params[:query], index_name: [Item.searchkick_index.name, Location.searchkick_index.name])

Searchkick will then search both Item and Location models, in whatever fields you have defined in the the SearchKick configuration for those models.

NeilS
  • 592
  • 3
  • 14