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']
@products = Array.new
results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
flash[:success] = "Success!"
end end
@products = Product.search(params[:search]).paginate(page: params[:page], :per_page => 50) rescue Exception => e
flash[:success] = "Success!" flash[:danger] = "No Result!"
else redirect_to action: :index
flash[:danger] = "Danger!"
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> <%= image_tag(product.image, size: "200x250")%>
<%= image_tag(product.image, size: "200x250")%> <%= simple_format(truncate(product.name, length:21)) %>
<%= simple_format(truncate(product.name, length:21)) %>
</div>
<div>
<p> Price: <%= price = (product.price/100.to_f) %> </p>
</br>
<%= link_to "Edit", "/admins/products/#{product.id}", class: "btn btn-lg btn-primary" %>
<%= link_to "delete", "/admins/destroy/#{product.id}", class: "btn btn-lg btn-danger" %>
</form>
</div>
</div> </div>
<% end %> <div>
</div> <p> Price: <%= price = (product.price/100.to_f) %> </p>
<%= will_paginate %> </br>
\ No newline at end of file <%= link_to "Edit", "/admins/products/#{product.id}", class: "btn btn-lg btn-primary" %>
<%= link_to "delete", "/admins/destroy/#{product.id}", class: "btn btn-lg btn-danger" %>
</form>
</div>
</div>
<% end %>
</div>
\ 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>
<%= image_tag(product.image, size: "200x250")%> <%= image_tag(product.image, size: "200x250")%>
<%= simple_format(truncate(product.name, length:21)) %> <%= simple_format(truncate(product.name, length:21)) %>
<%= (product.price/100.to_f).to_s + "$" %> <%= (product.price/100.to_f).to_s + "$" %>
</div>
<div>
<form action="/cart_products" style="padding-left : 0px" >
<input type="number" name="quantity" value= "1" min="1" max="100">
</br>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<%= link_to "Detail", "/products/#{product.id}", class: "btn btn-lg btn-info" %>
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
</div> </div>
<% end %> <div>
</div> <form action="/cart_products" style="padding-left : 0px" >
<%= will_paginate %> <input type="number" name="quantity" value= "1" min="1" max="100">
\ No newline at end of file </br>
<input type="submit" value="Add" class = "btn btn-lg btn-success" />
<%= link_to "Detail", "/products/#{product.id}", class: "btn btn-lg btn-info" %>
<input type="hidden" name="id" value="<%= product.id %>"/>
</form>
</div>
</div>
<% end %>
</div>
\ 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