I teach RoR. I add to the site search field (in my case, search for movies). Searching is done using the searchkick heme. But when I try to search, it gives me the following error: Must specify fields to search. What did I do:

  1. Installed Elasticsearch, checked. Everything works, if you make a request for port 9200, it gives:

name "95uHTn_" cluster_name "elasticsearch" cluster_uuid "pIiQyXbfRouPMmL7oNUxMA" version number "6.1.3" build_hash "af51318" build_date "2018-01-26T18:22:55.523Z" build_snapshot false lucene_version "7.1.0" minimum_wire_compatibility_version "5.6.0" minimum_index_compatibility_version "5.0.0" tagline "You Know, for Search"

Elasticsearch - works.

  1. Installed the searchkick 2.4 gem
  2. In the model Movie added

class Movie < ApplicationRecord searchkick end

  1. Routs

Rails.application.routes.draw do devise_for :users resources :movies do collection do get 'search' end resources :reviews, except: [:show, :index] end root 'movies#index' end

  1. On the form

<%= form_tag search_movies_path, method: :get, class: "navbar-form navbar-right", role: "search" do %> <p> <%= text_field_tag :search, params[:search], class: "form-control" %> <%= submit_tag "Search", name: nil, class: "btn btn-default" %> </p> <% end %>

  1. And in the controller Movie

def search if params[:search].present? @movies = Movie.search(params[:search]) else @movies = Movie.all end end

  1. Indexed the lookup tables first so

rake searchkick:reindex CLASS=Movie

then

rake searchkick:reindex:all

In theory, everything should work. But the error Must specify fields to search is still in the controller on the line

@movies = Movie.search(params[:search])

Checked with byebug. The search parameters are passed the value from the search form. I can not understand the reason. Here from the debugger

@_params = <ActionController::Parameters {"utf8"=>"✓", "search"=>"Accident Man", "controller"=>"movies", "action"=>"search"} permitted: false>

Only here is permitted: false . But I did not set any access restrictions!

  • The limit is the default. Have you ever seen the records params.require(сущность).permit(поля) ? And the answer to your question, in appearance, is already in Getting Started . - D-side
  • Saw. I've already flipped through the Getting Started several times, but I don’t see the focus (( - Serega Bondarenko
  • products = Product.search("apples", fields: [:name]) ? - D-side
  • Saw)) Thank you! @movies = Movie.search(params[:search], fields: [:title]) and everything works !!! - Serge Bondarenko
  • Write the answer then. - D-side

1 answer 1

It was necessary to specify the fields in which to search!

 @movies = Movie.search(params[:search], fields: [:title])