Commit c4a3614e by Tran Hoang Viet

VietTH: Implement feature search

parent d570482e
...@@ -69,4 +69,6 @@ gem 'mini_magick', '~> 3.8.1' ...@@ -69,4 +69,6 @@ gem 'mini_magick', '~> 3.8.1'
gem 'sidekiq', '~> 3.4.1' gem 'sidekiq', '~> 3.4.1'
# pagination # pagination
gem 'kaminari', '~> 0.16.3' gem 'kaminari', '~> 0.16.3'
\ No newline at end of file
gem 'rsolr', '~> 1.0.12'
\ No newline at end of file
...@@ -177,6 +177,8 @@ GEM ...@@ -177,6 +177,8 @@ GEM
request_store (1.1.0) request_store (1.1.0)
responders (2.1.0) responders (2.1.0)
railties (>= 4.2.0, < 5) railties (>= 4.2.0, < 5)
rsolr (1.0.12)
builder (>= 2.1.2)
ruby_parser (3.7.0) ruby_parser (3.7.0)
sexp_processor (~> 4.1) sexp_processor (~> 4.1)
sass (3.4.15) sass (3.4.15)
...@@ -259,6 +261,7 @@ DEPENDENCIES ...@@ -259,6 +261,7 @@ DEPENDENCIES
pry-rails (~> 0.3.4) pry-rails (~> 0.3.4)
quiet_assets quiet_assets
rails (= 4.2.3) rails (= 4.2.3)
rsolr (~> 1.0.12)
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
settingslogic (~> 2.0.9) settingslogic (~> 2.0.9)
...@@ -270,3 +273,6 @@ DEPENDENCIES ...@@ -270,3 +273,6 @@ DEPENDENCIES
unicorn unicorn
vacuum (~> 1.3.0) vacuum (~> 1.3.0)
web-console (~> 2.0) web-console (~> 2.0)
BUNDLED WITH
1.10.5
...@@ -30,6 +30,11 @@ class ProductsController < ApplicationController ...@@ -30,6 +30,11 @@ class ProductsController < ApplicationController
redirect_to @product redirect_to @product
end end
def search
query = params[:query].present? ? {title: params[:query]} : '*'
@products = Product.search(query, page: params[:page])
end
private private
def set_product def set_product
@product = Product.find_by(id: params[:id]) @product = Product.find_by(id: params[:id])
......
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
include Solr
# scopes # scopes
scope :recommended, ->{ self.offset(rand(self.count)).limit(Settings.limit_product_recommended) } scope :recommended, ->{ self.offset(rand(self.count)).limit(Settings.limit_product_recommended) }
scope :newest, ->{ self.order(created_at: :desc).limit(Settings.limit_product_newest) } scope :newest, ->{ self.order(created_at: :desc).limit(Settings.limit_product_newest) }
...@@ -14,4 +16,6 @@ class Product < ActiveRecord::Base ...@@ -14,4 +16,6 @@ class Product < ActiveRecord::Base
enum product_type: %i(system amazon) enum product_type: %i(system amazon)
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader
solr_options per_page: 2
end end
module Solr extend ActiveSupport::Concern
module ClassMethods
attr_accessor :solr_per_page, :solr_page, :solr_total_count
def solr_options(fields = {})
@solr_per_page = fields[:per_page] || 5
end
def search(query, options = {})
return unless query.is_a?(Hash)
@solr_page = options[:page] || 1
query = query.inject('') { |str, item| str << "#{item.first}:#{item.last}" << ' ' }
results = rsolr.paginate(
solr_page,
solr_per_page,
'select',
params: {q: query, fl: self.primary_key}
)
@solr_total_count = results['response']['numFound'].to_i
parse(results['response'])
end
def parse(results)
ids = results['docs'].map { |item| item[self.primary_key].to_i }
self.where(self.primary_key => ids)
end
def rsolr
@rsolr_service ||= RSolr.connect(url: ENV['SOLR_URL'])
end
def kaminari_paginate
Kaminari.paginate_array([], total_count: solr_total_count).page(solr_page).per(solr_per_page)
end
end
end
\ No newline at end of file
.input-group = form_tag search_products_path, method: :get do
%input.form-control{placeholder: "Search for..."} .input-group
= text_field_tag :query, params[:query], class: 'form-control', placeholder: 'Search for title...'
%span.input-group-btn %span.input-group-btn
%button.btn.btn-default{type: "button"} Go! %button.btn.btn-default{type: "submit"} Go!
\ No newline at end of file
%h3.title
Search result for:
= params[:query]
.module
- if @products.present?
#category-items.products-list-detail
= render @products
.pagination
= paginate @products.kaminari_paginate
\ No newline at end of file
# Amazon # Amazon
AWS_ACCESS: AKIAJ77C4CTZOP7TUVWQ AWS_ACCESS: AKIAJ77C4CTZOP7TUVWQ
AWS_SECRET: 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 AWS_SECRET: cYJYb/MLGV0M6oi1+DjlliL1cfxmh78tKXnT6ZmX
AWS_TAG: zigexn6400-22 AWS_TAG: zigexn6400-22
\ No newline at end of file
SOLR_URL: http://localhost:8080/solr/venshop
\ No newline at end of file
...@@ -6,6 +6,10 @@ Rails.application.routes.draw do ...@@ -6,6 +6,10 @@ Rails.application.routes.draw do
member do member do
get :add_cart get :add_cart
end end
collection do
get :search
end
end end
resources :categories resources :categories
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment