Commit 9b580163 by Hoang Phuc Do

Refactor code

parent 49c4ea61
...@@ -13,7 +13,7 @@ class ProductsController < ApplicationController ...@@ -13,7 +13,7 @@ class ProductsController < ApplicationController
def create def create
@product = Product.new(product_params.merge(user_id: current_user.id)) @product = Product.new(product_params.merge(user_id: current_user.id))
if @product.save if @product.save
@solr.add_product_index(@product) @solr.add_product(@product)
redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully created" } redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully created" }
else else
render 'new' render 'new'
...@@ -23,7 +23,7 @@ class ProductsController < ApplicationController ...@@ -23,7 +23,7 @@ class ProductsController < ApplicationController
# PATCH/PUT /products/1 # PATCH/PUT /products/1
def update def update
if @product.update(product_params) if @product.update(product_params)
@solr.update_product_index(@product) @solr.update_product(@product)
redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully updated" } redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully updated" }
else else
render 'edit' render 'edit'
...@@ -34,7 +34,7 @@ class ProductsController < ApplicationController ...@@ -34,7 +34,7 @@ class ProductsController < ApplicationController
def destroy def destroy
if @product.destroy if @product.destroy
flash[:success] = "Product #{@product.title} deleted" flash[:success] = "Product #{@product.title} deleted"
@solr.delete_product_index(@product) @solr.delete_product(@product)
else else
flash[:alert] = "Product #{@product.title} can't be deleted" flash[:alert] = "Product #{@product.title} can't be deleted"
end end
......
class ProductFilter class ProductFilter
def filter_product_title(title) def self.filter_search_query(query)
title.blank? ? 'title:*' : "title:#{RSolr.solr_escape(title)}" return '*' if query.blank?
filtered_query = RSolr.solr_escape(query)
filtered_query[/\D+/] == filtered_query ? "title:#{filtered_query}" : filtered_query
end end
def filter_suggest_keyword(keyword) def self.filter_suggest_keyword(keyword)
RSolr.solr_escape(keyword) RSolr.solr_escape(keyword)
end end
end end
\ No newline at end of file
...@@ -5,7 +5,8 @@ class Search ...@@ -5,7 +5,8 @@ class Search
# Get matched products with keyword # Get matched products with keyword
def products(search_query) def products(search_query)
matched_products(ProductFilter.filter_product_title(search_query)) @products_indexes = @solr.exec_select_request(ProductFilter.filter_search_query(search_query))
matched_products(@products_indexes)
end end
# Get suggested keywords # Get suggested keywords
......
...@@ -17,28 +17,28 @@ class Solr ...@@ -17,28 +17,28 @@ class Solr
response['suggest']['productSuggester'][query]['suggestions'] response['suggest']['productSuggester'][query]['suggestions']
end end
def add_products_indexes(products) def add_products(products)
rsolr.add products rsolr.add products
end end
def update_product_index(product) def update_product(product)
add_product_index(product) add_product(product)
delete_product_index(product) delete_product(product)
end end
def add_product_index(product) def add_product(product)
rsolr.add(id: product.id, rsolr.add(id: product.id,
title: product.title, title: product.title,
price: product.price) price: product.price)
rsolr.optimize rsolr.optimize
end end
def delete_product_index(product) def delete_product(product)
rsolr.delete_by_id product.id rsolr.delete_by_id product.id
rsolr.optimize rsolr.optimize
end end
def delete_all_product_indexes def delete_all_products
rsolr.update(data: '<delete><query>*:*</query></delete>', rsolr.update(data: '<delete><query>*:*</query></delete>',
headers: { 'Content-Type' => 'text/xml', headers: { 'Content-Type' => 'text/xml',
'charset' => 'utf-8' }) 'charset' => 'utf-8' })
......
...@@ -7,11 +7,11 @@ namespace :vs_solr do ...@@ -7,11 +7,11 @@ namespace :vs_solr do
title: product.title, title: product.title,
price: product.price } price: product.price }
end end
Solr.new.add_products_indexes(products) Solr.new.add_products(products)
end end
desc 'Remove all index from Solr' desc 'Remove all index from Solr'
task remove: :environment do task remove: :environment do
Solr.new.delete_all_product_indexes Solr.new.delete_all_products
end end
end end
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