Commit 8caff564 by Nguyen Quoc Kien

Index solr

parent 52718197
......@@ -30,6 +30,7 @@ gem "figaro"
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'solr-ruby'
gem 'rsolr', '~> 1.0.12'
# Use ActiveModel has_secure_password
......
......@@ -127,6 +127,8 @@ GEM
rdoc (4.2.0)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rsolr (1.0.12)
builder (>= 2.1.2)
sass (3.4.16)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
......@@ -185,6 +187,7 @@ DEPENDENCIES
jquery-rails
mysql2
rails (= 4.2.2)
rsolr (~> 1.0.12)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
solr-ruby
......
class Admin::ProductsController < ApplicationController
require 'net/http'
require 'uri'
before_action :find_product, only: [:destroy, :edit,:update]
before_action :get_categories, only: [:edit,:update, :new,:create]
before_action :authenticate_admin!
......@@ -11,6 +14,7 @@ class Admin::ProductsController < ApplicationController
def destroy
if @product.destroy
Solr.new.delete_solr_index_after_delete_product(params[:id])
flash[:success] = "Delete product : Success"
else
flash[:danger] = "Delete product : Error - Product add to carts"
......@@ -25,6 +29,7 @@ class Admin::ProductsController < ApplicationController
def create
@product = Product.new(product_params)
if @product.save
Solr.new.create_solr_index_after_create_product(@product.id, @product.name, @product.price, @product.description)
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
......@@ -34,6 +39,7 @@ class Admin::ProductsController < ApplicationController
def update
if @product.update(product_params)
Solr.new.update_solr_index_after_import_product(params[:id], product_params[:name])
flash[:success] = "Update product : Success"
redirect_to admin_products_path
else
......@@ -50,4 +56,5 @@ class Admin::ProductsController < ApplicationController
def get_categories
@categories = Category.all
end
end
require 'solr'
require 'rsolr'
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
......@@ -44,10 +45,15 @@ class ApplicationController < ActionController::Base
end
def find_product
if params[:id].to_i > (Product.count + 1)
redirect_to error_path
elsif is_number?(params[:id]) == false
if is_number?(params[:id]) == false
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
@product = Product.find(params[:id])
end
......
......@@ -13,16 +13,12 @@ class SearchController < ApplicationController
def search
@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}))"
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_products = get_result_solr(@result_total)
@products = get_products(@result_products)
else
@products = []
end
@result_total = @solr.send(select_obj)
@result_products = get_result_solr(@result_total)
@products = get_products(@result_products)
end
private
......@@ -40,4 +36,9 @@ class SearchController < ApplicationController
return products
end
def escape_characters_in_string(keyword)
pattern = /(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\"|\~|\*|\?|\:|\\)/
return keyword.gsub(pattern){|match|"\\" + match}
end
end
class Product < ActiveRecord::Base
default_scope -> { order(created_at: :desc) }
belongs_to :category
......@@ -16,7 +15,6 @@ class Product < ActiveRecord::Base
before_destroy :ensure_not_referenced_by_any_cart_product
before_save :convert_data_product
def self.search(keyword)
Product.where("name like ?", "%#{keyword}%" )
end
......
......@@ -4,7 +4,7 @@
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= image_tag(product.image, alt: product.name, style: 'height: 50px') %></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) %>
</th>
</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