Rails uses multiple field search

I recently added searchkickto my web application, but the study only works with name fields. There is my code:

MODEL:

class Campaign
 [...]

   searchkick language: "French"

 [...]

  def self.search_fields(params)
    @query = "*#{params[:search]}*"
    @search = Campaign.search @query, fields: [:name, :categories, :professional, :brief] , misspellings: {distance: 2}
    @search.results
  end

But, with

@search = Campaign.search @query, fields: [:name, :categories, :professional, :brief] , misspellings: {distance: 2}

The study only works with the name of each model. But I want to search in other fields, for example: categories ,: professional ,: brief, date, etc.

How can i do this?

+4
source share
1 answer

You must determine which fields you want to index in the model.

class Campaign
  [...]
  def search_data
    {
      name: name,
      categories: categories,
      professional: professional,
      brief: brief
    }
  end
  [...]
end

remember to override this model after the changes above.

ref: https://github.com/ankane/searchkick#indexing

+1
source

Source: https://habr.com/ru/post/1543567/


All Articles