0
votes

I used gem 'searchkick' for searching in my application.

here is my association and searchkick settings .

product.rb

belongs_to :sub_category
belongs_to :brand
belongs_to :product_type
has_one :category, through: :sub_category


searchkick match: :word_start,word_start: [:name], suggest: [:name]
  scope :search_import, -> { includes(:brand, :sub_category, :category, :product_type) }

def search_data
  {
    name: name,
    category_name: category.name,
    sub_category_name: sub_category.name,
    brand: brand.name,
    product_type: product_type.name
  }
end

I don't how searchkick works exactly. but i want to show the category name, sub category name and product type name in search results.

For an example,

I have list of alcohols products which belongs to different categories like beer, wine, whiskey etc.

if i search for beer, it should show beer in search results because beer is category which associated to product. i don't want the products which are associated to beer category, i want to beer category in search results.

here is my query for search query

response =  Product.search( params[:query], suggest: true, fields: ["name^10", "description"], limit: 5, operator: "or",misspellings: {below: 5} ).results

it is similar functionality of any e commerce application, like if i search iPhone 7 in flipkart application it will show iPhone 7 as category in search result, if i click on that all products related to iPhone 7 will show in one page.

i don't know how to achieve this , any help will appreciated.

2

2 Answers

0
votes

I think you are looking for aggregation feature. Just look the searchkick doc, you will find your answer.

Note: I am giving answer from Stackoverflow app. So I will later update it with some code from laptop.

0
votes

I have to do multi search , Here is the service which i made for getting associated model with multi search.

module Search
  class RetriveData
   def self.multi_search(params)
     @params = params
     create_searchable_variables_for_multi_model_search
     Searchkick.multi_search(array_of_searchkick_objects)
     array_of_searchkick_objects.map(&:results).flatten
   end

  private

  def self.array_of_searchkick_objects
    array_of_models.map do |klass|
      instance_variable_get("@#{klass.to_s.downcase}s")
    end
  end

  def self.searchable_fields
    ["name^10","sub_category_name","keyword_name",
     "category_name","product_type_name","brand_name"]
  end

  def self.create_searchable_variables_for_multi_model_search
    array_of_models.each do |klass|
      instance_variable_set("@#{klass.to_s.downcase}s",
                            klass.search( @params[:query], 
      constraints(klass) ))
    end
  end

  def self.constraints(klass)
    {
      fields: searchable_fields,
      suggest: true,
      operator: "or",
      misspellings: {below: 5},
      where: klass == Product ? { or: [[{available_cities_name: 
             @params[:city]},{available_cities_short_name: 
             @params[:short_name]}]] } : {},
      execute: false
    }
  end

  def self.array_of_models
    [Category, SubCategory, Brand, ProductType, Product]
  end
 end
end

Source is here