Commit f6eae475 by Vy Quoc Vu

solr

parent 23c96f32
...@@ -6,7 +6,7 @@ gem 'will_paginate-bootstrap' ...@@ -6,7 +6,7 @@ gem 'will_paginate-bootstrap'
gem 'bootstrap-will_paginate', '0.0.10' gem 'bootstrap-will_paginate', '0.0.10'
gem 'faker', '1.4.2' gem 'faker', '1.4.2'
gem 'sunspot_rails' gem 'sunspot_rails'
gem 'solr-ruby'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3' gem 'rails', '4.2.3'
......
...@@ -187,6 +187,7 @@ GEM ...@@ -187,6 +187,7 @@ GEM
rdoc (~> 4.0) rdoc (~> 4.0)
shellany (0.0.1) shellany (0.0.1)
slop (3.6.0) slop (3.6.0)
solr-ruby (0.0.8)
spring (1.3.6) spring (1.3.6)
sprockets (3.2.0) sprockets (3.2.0)
rack (~> 1.0) rack (~> 1.0)
...@@ -246,6 +247,7 @@ DEPENDENCIES ...@@ -246,6 +247,7 @@ DEPENDENCIES
rails_12factor (= 0.0.2) rails_12factor (= 0.0.2)
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
solr-ruby
spring spring
sunspot_rails sunspot_rails
sunspot_solr sunspot_solr
......
require 'solr'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
before_action :call_category , only: [:show, :index] before_action :call_category , only: [:show, :index]
before_action :configure_permitted_parameters, if: :devise_controller? before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
......
...@@ -22,16 +22,21 @@ class ProductsController < ApplicationController ...@@ -22,16 +22,21 @@ class ProductsController < ApplicationController
end end
def search def search
if params[:search] @solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on )
if !params[:page] || (params[:page].to_i == 0) query = "((name:#{params[:search]}))"
params[:page] = 1 select_obj = Solr::Request::Select.new(nil, {'q' => query})
else begin
params[:page] results = @solr.send(select_obj).data['response']['docs']
end @products = Array.new
@products = Product.search(params[:search]).paginate(page: params[:page], :per_page => 50) results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
flash[:success] = "Success!" flash[:success] = "Success!"
else end
flash[:danger] = "Danger!" rescue Exception => e
flash[:danger] = "No Result!"
redirect_to action: :index
end end
end end
......
<%= will_paginate %> <div class="row">
<div class="row">
<% @products.each do |product| %> <% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px"> <div class="col-md-4" style="padding-top : 20px">
<div> <div>
...@@ -15,5 +14,4 @@ ...@@ -15,5 +14,4 @@
</div> </div>
</div> </div>
<% end %> <% end %>
</div> </div>
<%= will_paginate %> \ No newline at end of file
\ No newline at end of file
<%= will_paginate %>
<div class="row"> <div class="row">
<% @products.each do |product| %> <% @products.each do |product| %>
<div class="col-md-4" style="padding-top : 20px"> <div class="col-md-4" style="padding-top : 20px">
<div> <div>
...@@ -18,5 +18,4 @@ ...@@ -18,5 +18,4 @@
</div> </div>
</div> </div>
<% end %> <% end %>
</div> </div>
<%= will_paginate %> \ No newline at end of file
\ No newline at end of file
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
</div> </div>
<div class="col-md-9" > <div class="col-md-9" >
<div class="form-group"> <div class="form-group">
<%= will_paginate %>
<%= render 'products/custom_products' %> <%= render 'products/custom_products' %>
<%= will_paginate %>
</div> </div>
</div> </div>
\ No newline at end of file
...@@ -8,6 +8,7 @@ Rails.application.configure do ...@@ -8,6 +8,7 @@ Rails.application.configure do
# Do not eager load code on boot. # Do not eager load code on boot.
config.eager_load = false config.eager_load = false
config.solr_host = "http://localhost:8080/solr/core0"
# Show full error reports and disable caching. # Show full error reports and disable caching.
config.consider_all_requests_local = true config.consider_all_requests_local = true
......
...@@ -9,7 +9,7 @@ Rails.application.configure do ...@@ -9,7 +9,7 @@ Rails.application.configure do
# and those relying on copy on write to perform better. # and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance. # Rake tasks automatically ignore this option for performance.
config.eager_load = true config.eager_load = true
config.solr_host = "http://localhost:8080/solr/core0"
# Full error reports are disabled and caching is turned on. # Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false config.consider_all_requests_local = false
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
......
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