Commit efdd6311 by Vy Quoc Vu

update-solr

parent f6eae475
......@@ -5,9 +5,8 @@ gem 'bootstrap-sass', '3.2.0.0'
gem 'will_paginate-bootstrap'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'faker', '1.4.2'
gem 'sunspot_rails'
gem 'solr-ruby'
gem 'rsolr'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use mysql as the database for Active Record
......
......@@ -127,7 +127,6 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
pr_geohash (1.0.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
......@@ -195,13 +194,6 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sunspot (2.2.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)
sunspot_rails (2.2.0)
nokogiri
rails (>= 3)
sunspot (= 2.2.0)
sunspot_solr (2.2.0)
thor (0.19.1)
thread_safe (0.3.5)
......@@ -245,11 +237,11 @@ DEPENDENCIES
mysql2
rails (= 4.2.3)
rails_12factor (= 0.0.2)
rsolr
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
solr-ruby
spring
sunspot_rails
sunspot_solr
turbolinks
uglifier (>= 1.3.0)
......
class Admins::ProductsController < ApplicationController
before_action :sorl_init, only: [:create,:update, :destroy]
def sorl_init
@solr = RSolr.connect :url => Rails.configuration.solr_host.to_s
end
def new
if admin_signed_in?
@product = Product.new
......@@ -29,6 +36,14 @@ class Admins::ProductsController < ApplicationController
product.price = product_params[:price].to_i*100
product.description = product_params[:description]
end
docs = [{:id =>product.id,
:price => product.price,
:name => product.name,
:description => product.description
}]
@solr.add docs
@solr.commit
@solr.optimize
flash[:success] = "Success!"
redirect_to "/products/#{product.id}"
else
......@@ -43,7 +58,9 @@ class Admins::ProductsController < ApplicationController
def edit
if admin_signed_in?
if product_params[:price].to_i > 0 && params[:category][:id].to_i > 0 &&
if product_params && product_params[:price].to_i > 0 &&
params[:category][:id].to_i > 0 &&
!product_params[:image].nil? && !product_params[:image].empty? &&
!product_params[:price].empty? && !product_params[:name].empty? &&
product_params[:price].to_i <= 999999
......@@ -53,12 +70,26 @@ class Admins::ProductsController < ApplicationController
product.category_id = params[:category][:id]
product.price = product_params[:price].to_i*100
product.description = product_params[:description]
product.save
if product.save
docs = [{:id =>product.id,
:price => product.price,
:name => product.name,
:description => product.description
}]
@solr.delete_by_query "id:#{product.id}"
@solr.add docs
@solr.commit
@solr.optimize
end
flash[:success] = "Success!"
redirect_to "/products/#{product.id}"
else
flash[:danger] = "Wrong params! Please check name, price and image again."
redirect_to :back
if !product_params
redirect_to :action => :index
else
redirect_to :back
end
end
else
flash[:danger] = "only admin!"
......@@ -68,8 +99,12 @@ class Admins::ProductsController < ApplicationController
def destroy
if admin_signed_in?
byebug
@product = Product.find(params[:id])
@product.destroy
@solr.delete_by_query "id:#{params[:id]}"
@solr.commit
@solr.optimize
flash[:success] = "Deleted!"
redirect_to :back
else
......@@ -89,8 +124,9 @@ class Admins::ProductsController < ApplicationController
private
def product_params
params.require(:product).permit(:name, :price, :description,
:image, :id)
if params[:product]
params.require(:product).permit(:name, :price, :description,:image, :id)
end
end
end
\ No newline at end of file
require 'solr'
require 'rsolr'
require 'rubygems'
class ApplicationController < ActionController::Base
before_action :call_category , only: [:show, :index]
before_action :configure_permitted_parameters, if: :devise_controller?
......
......@@ -21,22 +21,34 @@ class ProductsController < ApplicationController
end
end
def check_search_params(search_params)
special_characters = ["+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "~", ":",'\\','/']
@ok_string = ""
search_params.each_char do |key|
special_characters.each do |ch|
key = "\\#{ch}" if key == ch
end
@ok_string += key
end
end
def search
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on )
query = "((name:#{params[:search]}))"
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on)
check_search_params(params["search_params"])
query = "((name:#{@ok_string}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query})
begin
results = @solr.send(select_obj).data['response']['docs']
@products = Array.new
results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
flash[:success] = "Success!"
end
rescue Exception => e
flash[:danger] = "No Result!"
redirect_to action: :index
results = @solr.send(select_obj).data['response']['docs']
@products = Array.new
results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
end
if @products.empty?
flash.now[:danger] = "No result!"
else
flash.now[:success] = "All result!"
end
end
......
......@@ -8,7 +8,7 @@
<td><a class="navbar-brand" href= "/" > Venshop </a></td>
<td></td>
<form action= "/search" style="padding-left : 100px ; resize: vertical;" method="get" class="navbar-brand" >
<td ><input type="text" name="search" value="" , class="form-control"/></td>
<td ><input type="text" name="search_params" value="" , class="form-control"/></td>
<td style="padding-left : 20px" ><input type="submit" value="Search" style="font-size: 1em;" , class="btn btn-primary"/></td>
</form>
</table>
......
......@@ -7,6 +7,7 @@ Rails.application.configure do
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
config.solr_host = "http://localhost:8080/solr/core0"
......
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