Commit 9b580163 by Hoang Phuc Do

Refactor code

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