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
6270ece4
Commit
6270ece4
authored
Jun 23, 2017
by
Hoang Phuc Do
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dhp_solr' into dhp_solr_fix
parents
b56289bd
6f589e39
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 @
6270ece4
...
...
@@ -2,6 +2,7 @@ class ProductsController < ApplicationController
before_action
:authenticate_user!
,
only:
[
:new
,
:edit
,
:create
,
:update
,
:destroy
]
before_action
:set_product
,
only: :show
before_action
:user_can_edit_product
,
only:
[
:edit
,
:update
,
:destroy
]
before_action
:set_search
,
only:
[
:create
,
:update
,
:destroy
]
# GET /products/new
def
new
...
...
@@ -12,6 +13,7 @@ class ProductsController < ApplicationController
def
create
@product
=
Product
.
new
(
product_params
.
merge
(
user_id:
current_user
.
id
))
if
@product
.
save
@search
.
add_product
(
@product
)
redirect_to
root_url
,
flash:
{
success:
"Product
#{
@product
.
title
}
is sucessfully created"
}
else
render
'new'
...
...
@@ -21,6 +23,7 @@ class ProductsController < ApplicationController
# PATCH/PUT /products/1
def
update
if
@product
.
update
(
product_params
)
@search
.
update_product
(
@product
)
redirect_to
root_url
,
flash:
{
success:
"Product
#{
@product
.
title
}
is sucessfully updated"
}
else
render
'edit'
...
...
@@ -31,6 +34,7 @@ class ProductsController < ApplicationController
def
destroy
if
@product
.
destroy
flash
[
:success
]
=
"Product
#{
@product
.
title
}
deleted"
@search
.
delete_product
(
@product
)
else
flash
[
:alert
]
=
"Product
#{
@product
.
title
}
can't be deleted"
end
...
...
@@ -56,4 +60,8 @@ class ProductsController < ApplicationController
@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?
end
def
set_search
@search
=
Search
.
new
end
end
\ No newline at end of file
app/controllers/search_controller.rb
View file @
6270ece4
...
...
@@ -13,10 +13,10 @@ class SearchController < ApplicationController
private
def
set_search_result
@search_result
=
if
params
[
:q
].
present?
Search
.
new
(
params
[
:q
]).
products
else
Product
.
all
search
=
Search
.
new
@search_result
=
search
.
products
(
params
[
:q
])
if
@search_result
.
blank?
@suggested_keywords
=
search
.
suggested_keywords
(
params
[
:q
])
end
end
end
\ No newline at end of file
app/helpers/search_helper.rb
View file @
6270ece4
module
SearchHelper
def
render_search_result
(
search_result
)
def
render_search_result
(
search_result
,
suggested_keywords
)
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
render
(
partial:
'products/product_list'
,
locals:
{
products:
search_result
})
...
...
app/services/product_filter.rb
0 → 100644
View file @
6270ece4
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 @
6270ece4
class
Search
def
initialize
(
search_query
)
def
initialize
@solr
=
SolrSearch
.
new
@
search_query
=
search_query
@
product_filter
=
ProductFilter
.
new
(
@solr
)
end
def
products
@solr
.
search_for
(
@search_query
)
filter_products
(
@solr
.
docs
)
def
products
(
search_query
)
matched_products
(
@product_filter
.
filter_search_query
(
search_query
))
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
private
def
filter
_products
(
products
)
def
matched
_products
(
products
)
product_ids
=
products
.
map
{
|
product
|
product
[
:id
.
to_s
]
}
Product
.
find
(
product_ids
)
end
...
...
app/services/solr_search.rb
View file @
6270ece4
...
...
@@ -5,19 +5,39 @@ class SolrSearch
def
initialize
@rsolr
=
RSolr
.
connect
url:
Settings
.
rsolr
.
address
@response
=
nil
end
def
search_for
(
search_query
)
search_query
||=
''
@response
=
rsolr
.
get
'select'
,
params:
{
q:
sanitize_query
(
search_query
)
}
def
query_product_title
(
search_query
)
response
=
rsolr
.
get
'select'
,
params:
{
q:
"title:
#{
search_query
}
"
}
response
[
'response'
][
'docs'
]
end
def
docs
@response
[
'response'
][
'docs'
]
def
query_product_suggestion
(
search_query
)
response
=
rsolr
.
get
'suggest'
,
params:
{
'suggest.q'
=>
search_query
}
response
[
'suggest'
][
'productSuggester'
][
search_query
][
'suggestions'
]
end
def
sanitize_query
(
query
)
query
.
gsub
(
/[^0-9A-Za-z]/
,
''
)
def
update_product_index
(
product
)
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
\ No newline at end of file
app/views/layouts/header/_search_form.html.erb
View file @
6270ece4
...
...
@@ -2,7 +2,7 @@
<a
href=
"#"
class=
"search-toggle"
><i
class=
"fa fa-search"
></i></a>
<%=
form_tag
search_result_path
,
method: :get
do
%>
<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'
%>
</div>
<%
end
%>
...
...
app/views/search/show.html.erb
View file @
6270ece4
...
...
@@ -4,7 +4,7 @@
<h2
class=
"h2 heading-primary mt-lg clearfix"
>
<span>
Search result
</span>
</h2>
<%=
render_search_result
(
@products
)
%>
<%=
render_search_result
(
@products
,
@suggested_keywords
)
%>
<div
class=
"toolbar-bottom"
>
<div
class=
"toolbar"
>
...
...
lib/tasks/
import_solr_data
.rake
→
lib/tasks/
vs_solr
.rake
View file @
6270ece4
namespace
:
import_solr_data
do
desc
"Import product data from database"
namespace
:
vs_solr
do
desc
'Import product data from database'
task
product: :environment
do
documents
=
[]
Product
.
all
.
each
do
|
product
|
documents
<<
{
id:
product
.
id
,
title:
product
.
title
,
price:
product
.
price
}
end
solr_search
=
SolrSearch
.
new
solr_search
.
rsolr
.
add
documents
SolrSearch
.
new
.
rsolr
.
add
documents
end
desc
'Remove all index from Solr'
task
remove: :environment
do
SolrSearch
.
new
.
delete_all_product_indexes
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