Commit f6eae475 by Vy Quoc Vu

solr

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