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
6f589e39
Commit
6f589e39
authored
Jun 22, 2017
by
Hoang Phuc Do
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix search bug
parent
acbd7d6e
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
94 additions
and
26 deletions
+94
-26
app/controllers/products_controller.rb
+9
-0
app/controllers/search_controller.rb
+5
-4
app/helpers/search_helper.rb
+5
-2
app/services/product_filter.rb
+15
-0
app/services/search.rb
+21
-6
app/services/solr_search.rb
+29
-8
app/views/layouts/header/_search_form.html.erb
+1
-1
app/views/search/show.html.erb
+1
-1
lib/tasks/vs_solr.rake
+8
-4
No files found.
app/controllers/products_controller.rb
View file @
6f589e39
...
@@ -2,6 +2,7 @@ class ProductsController < ApplicationController
...
@@ -2,6 +2,7 @@ class ProductsController < ApplicationController
before_action
:authenticate_user!
,
only:
[
:new
,
:edit
,
:create
,
:update
,
:destroy
]
before_action
:authenticate_user!
,
only:
[
:new
,
:edit
,
:create
,
:update
,
:destroy
]
before_action
:set_product
,
only: :show
before_action
:set_product
,
only: :show
before_action
:user_can_edit_product
,
only:
[
:edit
,
:update
,
:destroy
]
before_action
:user_can_edit_product
,
only:
[
:edit
,
:update
,
:destroy
]
before_action
:set_search
,
only:
[
:create
,
:update
,
:destroy
]
# GET /products/new
# GET /products/new
def
new
def
new
...
@@ -12,6 +13,7 @@ class ProductsController < ApplicationController
...
@@ -12,6 +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
@search
.
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'
...
@@ -21,6 +23,7 @@ class ProductsController < ApplicationController
...
@@ -21,6 +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
)
@search
.
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'
...
@@ -31,6 +34,7 @@ class ProductsController < ApplicationController
...
@@ -31,6 +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"
@search
.
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
...
@@ -56,4 +60,8 @@ class ProductsController < ApplicationController
...
@@ -56,4 +60,8 @@ class ProductsController < ApplicationController
@product
=
current_user
.
products
.
find_by
(
id:
params
[
:id
])
@product
=
current_user
.
products
.
find_by
(
id:
params
[
:id
])
redirect_to
root_url
,
flash:
{
alert:
'You do not have permission to edit this product'
}
if
@product
.
blank?
redirect_to
root_url
,
flash:
{
alert:
'You do not have permission to edit this product'
}
if
@product
.
blank?
end
end
def
set_search
@search
=
Search
.
new
end
end
end
\ No newline at end of file
app/controllers/search_controller.rb
View file @
6f589e39
...
@@ -13,10 +13,10 @@ class SearchController < ApplicationController
...
@@ -13,10 +13,10 @@ class SearchController < ApplicationController
private
private
def
set_search_result
def
set_search_result
@search_result
=
if
params
[
:q
].
present?
search
=
Search
.
new
Search
.
new
(
params
[
:q
]).
products
@search_result
=
search
.
products
(
params
[
:q
])
else
if
@search_result
.
blank?
Product
.
all
@suggested_keywords
=
search
.
suggested_keywords
(
params
[
:q
])
end
end
end
end
end
end
\ No newline at end of file
app/helpers/search_helper.rb
View file @
6f589e39
module
SearchHelper
module
SearchHelper
def
render_search_result
(
search_result
)
def
render_search_result
(
search_result
,
suggested_keywords
)
if
search_result
.
blank?
if
search_result
.
blank?
render
html:
'Sorry! There are no products matched your query'
keys
=
suggested_keywords
.
map
{
|
kw
|
link_to
kw
,
search_result_url
(
q:
kw
)
}
.
\
to_sentence
(
last_word_connector:
' or '
)
suggestion_msg
=
keys
.
present?
?
" You can try different keywords like
#{
keys
}
"
:
''
render
html:
"Sorry! There are no products matched your query.
#{
suggestion_msg
}
"
.
html_safe
else
else
render
(
partial:
'products/product_list'
,
render
(
partial:
'products/product_list'
,
locals:
{
products:
search_result
})
locals:
{
products:
search_result
})
...
...
app/services/product_filter.rb
0 → 100644
View file @
6f589e39
class
ProductFilter
def
initialize
(
solr
)
@solr
=
solr
end
def
filter_search_query
(
search_query
)
search_query
=
search_query
.
blank?
?
'*'
:
RSolr
.
solr_escape
(
search_query
)
@solr
.
query_product_title
(
search_query
)
end
def
filter_suggest_query
(
suggest_query
)
@solr
.
query_product_suggestion
(
RSolr
.
solr_escape
(
suggest_query
))
end
end
\ No newline at end of file
app/services/search.rb
View file @
6f589e39
class
Search
class
Search
def
initialize
(
search_query
)
def
initialize
@solr
=
SolrSearch
.
new
@solr
=
SolrSearch
.
new
@
search_query
=
search_query
@
product_filter
=
ProductFilter
.
new
(
@solr
)
end
end
def
products
def
products
(
search_query
)
@solr
.
search_for
(
@search_query
)
matched_products
(
@product_filter
.
filter_search_query
(
search_query
))
filter_products
(
@solr
.
docs
)
end
def
suggested_keywords
(
keyword
)
@product_filter
.
filter_suggest_query
(
keyword
).
map
{
|
kw
|
kw
[
'term'
]
}
end
def
update_product
(
product
)
@solr
.
update_product_index
(
product
)
end
def
add_product
(
product
)
@solr
.
add_product_index
(
product
)
end
def
delete_product
(
product
)
@solr
.
delete_product_index
(
product
)
end
end
private
private
def
filter
_products
(
products
)
def
matched
_products
(
products
)
product_ids
=
products
.
map
{
|
product
|
product
[
:id
.
to_s
]
}
product_ids
=
products
.
map
{
|
product
|
product
[
:id
.
to_s
]
}
Product
.
find
(
product_ids
)
Product
.
find
(
product_ids
)
end
end
...
...
app/services/solr_search.rb
View file @
6f589e39
...
@@ -5,19 +5,39 @@ class SolrSearch
...
@@ -5,19 +5,39 @@ class SolrSearch
def
initialize
def
initialize
@rsolr
=
RSolr
.
connect
url:
Settings
.
rsolr
.
address
@rsolr
=
RSolr
.
connect
url:
Settings
.
rsolr
.
address
@response
=
nil
end
end
def
search_for
(
search_query
)
def
query_product_title
(
search_query
)
search_query
||=
''
response
=
rsolr
.
get
'select'
,
params:
{
q:
"title:
#{
search_query
}
"
}
@response
=
rsolr
.
get
'select'
,
params:
{
q:
sanitize_query
(
search_query
)
}
response
[
'response'
][
'docs'
]
end
end
def
docs
def
query_product_suggestion
(
search_query
)
@response
[
'response'
][
'docs'
]
response
=
rsolr
.
get
'suggest'
,
params:
{
'suggest.q'
=>
search_query
}
response
[
'suggest'
][
'productSuggester'
][
search_query
][
'suggestions'
]
end
end
def
sanitize_query
(
query
)
def
update_product_index
(
product
)
query
.
gsub
(
/[^0-9A-Za-z]/
,
''
)
add_product_index
(
product
)
delete_product_index
(
product
)
end
def
add_product_index
(
product
)
rsolr
.
add
(
id:
product
.
id
,
title:
product
.
title
,
price:
product
.
price
)
rsolr
.
optimize
end
def
delete_product_index
(
product
)
rsolr
.
delete_by_id
product
.
id
rsolr
.
optimize
end
def
delete_all_product_indexes
rsolr
.
update
(
data:
'<delete><query>*:*</query></delete>'
,
headers:
{
'Content-Type'
=>
'text/xml'
,
'charset'
=>
'utf-8'
})
rsolr
.
commit
end
end
end
end
\ No newline at end of file
app/views/layouts/header/_search_form.html.erb
View file @
6f589e39
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
<a
href=
"#"
class=
"search-toggle"
><i
class=
"fa fa-search"
></i></a>
<a
href=
"#"
class=
"search-toggle"
><i
class=
"fa fa-search"
></i></a>
<%=
form_tag
search_result_path
,
method: :get
do
%>
<%=
form_tag
search_result_path
,
method: :get
do
%>
<div
class=
"header-search-wrapper"
>
<div
class=
"header-search-wrapper"
>
<%=
text_field_tag
:q
,
params
[
:q
],
class:
'form-control'
,
placeholder:
'
Search
...'
%>
<%=
text_field_tag
:q
,
params
[
:q
],
class:
'form-control'
,
placeholder:
'
Enter product title
...'
%>
<%=
button_tag
raw
(
'<i class="fa fa-search"></i>'
),
class:
'btn btn-default'
%>
<%=
button_tag
raw
(
'<i class="fa fa-search"></i>'
),
class:
'btn btn-default'
%>
</div>
</div>
<%
end
%>
<%
end
%>
...
...
app/views/search/show.html.erb
View file @
6f589e39
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<span>
Search result
</span>
<span>
Search result
</span>
</h2>
</h2>
<%=
render_search_result
(
@products
)
%>
<%=
render_search_result
(
@products
,
@suggested_keywords
)
%>
<div
class=
"toolbar-bottom"
>
<div
class=
"toolbar-bottom"
>
<div
class=
"toolbar"
>
<div
class=
"toolbar"
>
...
...
lib/tasks/
import_solr_data
.rake
→
lib/tasks/
vs_solr
.rake
View file @
6f589e39
namespace
:
import_solr_data
do
namespace
:
vs_solr
do
desc
"Import product data from database"
desc
'Import product data from database'
task
product: :environment
do
task
product: :environment
do
documents
=
[]
documents
=
[]
Product
.
all
.
each
do
|
product
|
Product
.
all
.
each
do
|
product
|
documents
<<
{
id:
product
.
id
,
title:
product
.
title
,
price:
product
.
price
}
documents
<<
{
id:
product
.
id
,
title:
product
.
title
,
price:
product
.
price
}
end
end
solr_search
=
SolrSearch
.
new
SolrSearch
.
new
.
rsolr
.
add
documents
solr_search
.
rsolr
.
add
documents
end
desc
'Remove all index from Solr'
task
remove: :environment
do
SolrSearch
.
new
.
delete_all_product_indexes
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