Commit b1fdbaec by Nguyen Quoc Kien

Merge branch 'solr_search' into 'develop'

Search solr

See merge request !5
parents b9cc116f c9b28616
...@@ -29,7 +29,8 @@ gem 'vacuum', '~> 1.3.0' ...@@ -29,7 +29,8 @@ gem 'vacuum', '~> 1.3.0'
gem "figaro" gem "figaro"
gem 'will_paginate', '3.0.7' gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10' gem 'bootstrap-will_paginate', '0.0.10'
gem 'solr-ruby'
gem 'rsolr', '~> 1.0.12'
# Use ActiveModel has_secure_password # Use ActiveModel has_secure_password
......
...@@ -127,6 +127,8 @@ GEM ...@@ -127,6 +127,8 @@ GEM
rdoc (4.2.0) rdoc (4.2.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)
sass (3.4.16) sass (3.4.16)
sass-rails (5.0.3) sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
...@@ -137,6 +139,7 @@ GEM ...@@ -137,6 +139,7 @@ GEM
sdoc (0.4.1) sdoc (0.4.1)
json (~> 1.7, >= 1.7.7) json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0) rdoc (~> 4.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)
...@@ -184,8 +187,10 @@ DEPENDENCIES ...@@ -184,8 +187,10 @@ DEPENDENCIES
jquery-rails jquery-rails
mysql2 mysql2
rails (= 4.2.2) rails (= 4.2.2)
rsolr (~> 1.0.12)
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
solr-ruby
spring spring
turbolinks turbolinks
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
......
class Admin::ProductsController < ApplicationController class Admin::ProductsController < ApplicationController
require 'net/http'
require 'uri'
before_action :find_product, only: [:destroy, :edit,:update] before_action :find_product, only: [:destroy, :edit,:update]
before_action :get_categories, only: [:edit,:update, :new,:create] before_action :get_categories, only: [:edit,:update, :new,:create]
before_action :authenticate_admin! before_action :authenticate_admin!
...@@ -11,6 +14,7 @@ class Admin::ProductsController < ApplicationController ...@@ -11,6 +14,7 @@ class Admin::ProductsController < ApplicationController
def destroy def destroy
if @product.destroy if @product.destroy
SolrModule.new.delete_solr_index_after_delete_product(params[:id])
flash[:success] = "Delete product : Success" flash[:success] = "Delete product : Success"
else else
flash[:danger] = "Delete product : Error - Product add to carts" flash[:danger] = "Delete product : Error - Product add to carts"
...@@ -25,6 +29,7 @@ class Admin::ProductsController < ApplicationController ...@@ -25,6 +29,7 @@ class Admin::ProductsController < ApplicationController
def create def create
@product = Product.new(product_params) @product = Product.new(product_params)
if @product.save if @product.save
SolrModule.new.create_solr_index_after_create_product(@product.id, @product.name, @product.price, @product.description)
flash[:success] = "Create product : Success" flash[:success] = "Create product : Success"
redirect_to admin_products_path redirect_to admin_products_path
else else
...@@ -34,6 +39,7 @@ class Admin::ProductsController < ApplicationController ...@@ -34,6 +39,7 @@ class Admin::ProductsController < ApplicationController
def update def update
if @product.update(product_params) if @product.update(product_params)
SolrModule.new.update_solr_index_after_import_product(params[:id], product_params[:name])
flash[:success] = "Update product : Success" flash[:success] = "Update product : Success"
redirect_to admin_products_path redirect_to admin_products_path
else else
...@@ -50,4 +56,5 @@ class Admin::ProductsController < ApplicationController ...@@ -50,4 +56,5 @@ class Admin::ProductsController < ApplicationController
def get_categories def get_categories
@categories = Category.all @categories = Category.all
end end
end end
require 'solr'
require 'rsolr'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# 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.
...@@ -43,10 +45,15 @@ class ApplicationController < ActionController::Base ...@@ -43,10 +45,15 @@ class ApplicationController < ActionController::Base
end end
def find_product def find_product
if params[:id].to_i > (Product.count + 1) if is_number?(params[:id]) == false
redirect_to error_path
elsif is_number?(params[:id]) == false
redirect_to error_path redirect_to error_path
elsif !Product.exists?(params[:id])
flash[:danger] = "Product not found!"
if current_admin
redirect_to admin_products_path
else
redirect_to products_path
end
else else
@product = Product.find(params[:id]) @product = Product.find(params[:id])
end end
......
class SearchController < ApplicationController class SearchController < ApplicationController
before_action :check_page, only: [:search] before_action :check_page, only: [:search]
=begin
def search def search
if params[:keyword].nil? if params[:keyword].nil?
@products = [] @products = []
...@@ -8,4 +9,41 @@ class SearchController < ApplicationController ...@@ -8,4 +9,41 @@ class SearchController < ApplicationController
@products = Product.search(params[:keyword]).paginate(page: params[:page]).per_page(18) @products = Product.search(params[:keyword]).paginate(page: params[:page]).per_page(18)
end end
end end
=end
def search
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on )
keyword = escape_characters_in_string(params[:keyword])
query = "((name:#{keyword}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query, 'wt' => "xml", 'rows' => 2000, 'indent' => true})
@result_total = @solr.send(select_obj)
@result_products = get_result_solr(@result_total)
@products = get_products(@result_products)
end
private
def get_result_solr(result_total)
products = result_total.data['response']['docs']
end
def get_products(result_products)
products = Array.new
result_products.each do |iProduct|
product = Product.find(iProduct['id'])
products << product
end
return products
end
def escape_characters_in_string(keyword)
if keyword == ""
flash[:danger] = "Keyword is null"
redirect_to products_path
else
pattern = /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\"|\~|\*|\?|\:|\ |\\)/
return keyword.gsub(pattern){|match|"\\" + match}
end
end
end end
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
default_scope -> { order(created_at: :desc) } default_scope -> { order(created_at: :desc) }
belongs_to :category belongs_to :category
...@@ -16,7 +15,6 @@ class Product < ActiveRecord::Base ...@@ -16,7 +15,6 @@ class Product < ActiveRecord::Base
before_destroy :ensure_not_referenced_by_any_cart_product before_destroy :ensure_not_referenced_by_any_cart_product
before_save :convert_data_product before_save :convert_data_product
def self.search(keyword) def self.search(keyword)
Product.where("name like ?", "%#{keyword}%" ) Product.where("name like ?", "%#{keyword}%" )
end end
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<td><%= number_to_currency(product.price/100.00) %></td> <td><%= number_to_currency(product.price/100.00) %></td>
<td><%= image_tag(product.image, alt: product.name, style: 'height: 50px') %></td> <td><%= image_tag(product.image, alt: product.name, style: 'height: 50px') %></td>
<td><%= Category.find(product.category_id).name %> </td> <td><%= Category.find(product.category_id).name %> </td>
<th><%= link_to "Delete", admin_product_path(id: product.id), method: :delete, data: { confirm: 'Are you sure?' } %> <th><%= link_to "Delete", admin_product_path(id: product.id), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to "Edit", edit_admin_product_path(id: product.id) %> <%= link_to "Edit", edit_admin_product_path(id: product.id) %>
</th> </th>
</tr> </tr>
<% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2>
<%= will_paginate @products %>
<div class="col-md-12" style="text-align: center;">
<% if @products!= [] %>
<% @products.each do |product| %>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<%= image_tag(product.image, alt: product.name, style: 'height: 300px') %>
<div class="caption">
<div class="div_product_name" style="width: 235px; height: 53px;">
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<%= form_tag cart_products_path do %>
<p>
<%= hidden_field_tag :product_id, product.id %>
Quantity:<b> <%= number_field_tag :quantity, "1", class: 'form-control' %></b><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p>
<% end %>
<%= link_to "More Info", product, :class => "btn btn-default" %>
</div>
</div>
</div>
<% end %>
<% else %>
<h1> Not found</h1>
<% end %>
</div>
<div><%= will_paginate @products %></div>
<% provide(:title, "Search Products") %> <% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2> <h2 class="text-left">Search Products with keyword: "<%= params[:keyword] %>" - <%= @products.count %> resulfs</h2>
<%= will_paginate @products %>
<div class="col-md-12" style="text-align: center;"> <div class="col-md-12" style="text-align: center;">
<% if @products!= [] %> <% if @products!= [] %>
<% @products.each do |product| %> <% @products.each do |product| %>
...@@ -28,4 +28,4 @@ ...@@ -28,4 +28,4 @@
<h1> Not found</h1> <h1> Not found</h1>
<% end %> <% end %>
</div> </div>
<div><%= will_paginate @products %></div>
<h1>Pages not found!</h1> <div class="dialog">
\ No newline at end of file <div>
<h1>We're sorry, but something went wrong.</h1>
</div>
<p>If you are the application owner check the logs for more information.</p>
</div>
\ No newline at end of file
...@@ -36,6 +36,8 @@ Rails.application.configure do ...@@ -36,6 +36,8 @@ Rails.application.configure do
# Raises helpful error messages. # Raises helpful error messages.
config.assets.raise_runtime_errors = true config.assets.raise_runtime_errors = true
#solr
config.solr_host = "http://localhost:8080/solr/core0"
# Raises error for missing translations # Raises error for missing translations
# config.action_view.raise_on_missing_translations = true # config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
......
class SolrModule
def initialize
@solr = RSolr.connect :url => Rails.configuration.solr_host.to_s
end
def update_solr_index_after_import_product(id, name)
%x{curl 'localhost:8080/solr/core0/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"#{id}","name":{"set":"#{name}"}}]'}
end
def delete_solr_index_after_delete_product(id)
@solr.delete_by_id id
@solr.commit
@solr.optimize
end
def create_solr_index_after_create_product(id, name, price, description)
doc = [{:id => id,:name => name, :price => price, :description => description}]
@solr.add doc
@solr.commit
@solr.optimize
end
end
\ No newline at end of file
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