Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
dhp-venshop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Hoang Phuc Do
dhp-venshop
Commits
9b580163
Commit
9b580163
authored
Jun 23, 2017
by
Hoang Phuc Do
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor code
parent
49c4ea61
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20 additions
and
16 deletions
+20
-16
app/controllers/products_controller.rb
+3
-3
app/services/product_filter.rb
+6
-3
app/services/search.rb
+2
-1
app/services/solr.rb
+7
-7
lib/tasks/vs_solr.rake
+2
-2
No files found.
app/controllers/products_controller.rb
View file @
9b580163
...
@@ -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
...
...
app/services/product_filter.rb
View file @
9b580163
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
app/services/search.rb
View file @
9b580163
...
@@ -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
...
...
app/services/solr.rb
View file @
9b580163
...
@@ -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
_indexe
s
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'
})
...
...
lib/tasks/vs_solr.rake
View file @
9b580163
...
@@ -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
_indexe
s
Solr
.
new
.
delete_all_products
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment