Commit 8caff564 by Nguyen Quoc Kien

Index solr

parent 52718197
...@@ -30,6 +30,7 @@ gem "figaro" ...@@ -30,6 +30,7 @@ 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 '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)
...@@ -185,6 +187,7 @@ DEPENDENCIES ...@@ -185,6 +187,7 @@ 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 solr-ruby
......
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
Solr.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
Solr.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)
Solr.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 '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.
...@@ -44,10 +45,15 @@ class ApplicationController < ActionController::Base ...@@ -44,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
......
...@@ -13,16 +13,12 @@ class SearchController < ApplicationController ...@@ -13,16 +13,12 @@ class SearchController < ApplicationController
def search def search
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on ) @solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on )
keyword = params[:keyword] keyword = escape_characters_in_string(params[:keyword])
query = "((name:#{keyword}))" query = "((name:#{keyword}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query, 'wt' => "xml", 'rows' => 2000, 'indent' => true}) select_obj = Solr::Request::Select.new(nil, {'q' => query, 'wt' => "xml", 'rows' => 2000, 'indent' => true})
if keyword.index( /[^[:alnum:]]/ ) == nil @result_total = @solr.send(select_obj)
@result_total = @solr.send(select_obj) @result_products = get_result_solr(@result_total)
@result_products = get_result_solr(@result_total) @products = get_products(@result_products)
@products = get_products(@result_products)
else
@products = []
end
end end
private private
...@@ -40,4 +36,9 @@ class SearchController < ApplicationController ...@@ -40,4 +36,9 @@ class SearchController < ApplicationController
return products return products
end end
def escape_characters_in_string(keyword)
pattern = /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\"|\~|\*|\?|\:|\\)/
return keyword.gsub(pattern){|match|"\\" + match}
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>
class Solr
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