Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VeNJob
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
Tô Ngọc Ánh
VeNJob
Commits
4fa29101
Commit
4fa29101
authored
Sep 01, 2020
by
Tô Ngọc Ánh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
full-text search using solr
parent
ea725d57
Pipeline
#1052
failed with stages
in 0 seconds
Changes
8
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
66 additions
and
11 deletions
+66
-11
app/controllers/jobs_controller.rb
+13
-4
app/views/favorites/destroy.js.erb
+2
-2
app/views/jobs/_search_result.html.erb
+1
-1
app/views/jobs/index.html.erb
+1
-1
app/views/jobs/search.html.erb
+1
-1
app/views/shared/_searchbar.html.erb
+2
-2
lib/common/solr_server.rb
+21
-0
lib/tasks/solr.rake
+25
-0
No files found.
app/controllers/jobs_controller.rb
View file @
4fa29101
require
'./lib/common/solr_server.rb'
class
JobsController
<
ApplicationController
class
JobsController
<
ApplicationController
before_action
:get_data_search_bar
,
only:
%i[index search]
before_action
:get_data_search_bar
,
only:
%i[index search]
def
index
def
index
object
=
params
[
:model
].
classify
.
constantize
.
find_by_slug
(
params
[
:slug
])
object
=
params
[
:model
].
classify
.
constantize
.
find_by_slug
(
params
[
:slug
])
@keyword
=
object
.
try
(
:name
)
||
object
.
try
(
:city
)
@keyword
=
object
.
try
(
:name
)
||
object
.
try
(
:city
)
params
[
params
[
:model
]]
=
object
.
id
@jobs
=
object
.
jobs
.
all
.
includes
(
:company
,
:locations
).
page
(
params
[
:page
])
@jobs
=
object
.
jobs
.
all
.
includes
(
:company
,
:locations
).
page
(
params
[
:page
])
end
end
...
@@ -17,11 +20,10 @@ class JobsController < ApplicationController
...
@@ -17,11 +20,10 @@ class JobsController < ApplicationController
end
end
def
search
def
search
solr
=
RSolr
.
connect
(
url:
'http://localhost:8983/solr/venjob'
)
solr
=
SolrServer
.
new
query
=
params
[
:search
].
blank?
?
'*:*'
:
"title:
#{
params
[
:search
]
}
OR company:
#{
params
[
:search
]
}
"
results
=
solr
.
search
(
params
[
:search
],
params
[
:industry
],
params
[
:location
],
params
[
:page
])
results
=
solr
.
paginate
(
params
[
:page
],
Job
::
NUMBER_SEARCH_RESULTS
,
'select'
,
params:
{
q:
query
,
wt: :ruby
})
@paginatable_array
=
Kaminari
.
paginate_array
(
results
[
'response'
][
'docs'
],
total_count:
results
[
'response'
][
'numFound'
]).
page
(
params
[
:page
])
@paginatable_array
=
Kaminari
.
paginate_array
(
results
[
'response'
][
'docs'
],
total_count:
results
[
'response'
][
'numFound'
]).
page
(
params
[
:page
])
@keyword
=
params
[
:search
]
@keyword
=
set_search_keyword
(
params
[
:search
],
params
[
:industry
],
params
[
:location
])
end
end
private
private
...
@@ -35,4 +37,11 @@ class JobsController < ApplicationController
...
@@ -35,4 +37,11 @@ class JobsController < ApplicationController
@locations
=
Location
.
select
(
:id
,
:city
)
@locations
=
Location
.
select
(
:id
,
:city
)
@industries
=
Industry
.
select
(
:id
,
:name
)
@industries
=
Industry
.
select
(
:id
,
:name
)
end
end
def
set_search_keyword
(
keyword
,
industry_id
,
location_id
)
search_text
=
keyword
.
blank?
?
'All Jobs'
:
keyword
industry_name
=
@industries
.
detect
{
|
v
|
v
.
id
==
industry_id
.
to_i
}.
try
(
:name
)
unless
industry_id
.
blank?
city_name
=
@locations
.
detect
{
|
v
|
v
.
id
==
location_id
.
to_i
}.
try
(
:city
)
unless
location_id
.
blank?
"
#{
search_text
}
#{
industry_name
}
#{
city_name
}
"
end
end
end
app/views/favorites/destroy.js.erb
View file @
4fa29101
$('#favorite-
<%=
@favorite
.
job_id
%>
')
$('#favorite-
<%=
@favorite
.
job_id
%>
')
.html("
<%=
j
render
'favorites/link_favorite'
,
job_id:
@favorite
.
job_id
%>
")
.html("
<%=
j
render
'favorites/link_favorite'
,
job_id:
@favorite
.
job_id
%>
")
$('#sum-favorited').html("
<%=
current_user
.
favorites
.
size
%>
")
$('#sum-favorited').html("
<%=
current_user
.
favorites
.
size
%>
")
$('#favorited-
<%=
@favorite
.
job_id
%>
').remove()
var num_child = $('.form-group .form-item').length
var num_child = $('.form-group .form-item').length
if (num_child == 0) {
$('#favorited-
<%=
@favorite
.
job_id
%>
').remove()
if (num_child == 1) {
var prev = $('a[rel=prev]')
var prev = $('a[rel=prev]')
if (prev.length > 0) {
if (prev.length > 0) {
window.location = prev.attr('href')
window.location = prev.attr('href')
...
...
app/views/jobs/_search_result.html.erb
View file @
4fa29101
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<p
class=
'card-text'
>
<%=
search_result
[
'company'
]
%>
</p>
<p
class=
'card-text'
>
<%=
search_result
[
'company'
]
%>
</p>
<p
class=
'mb-0'
>
<p
class=
'mb-0'
>
<strong>
Work place:
</strong>
<strong>
Work place:
</strong>
<%=
search_result
[
'
city
'
].
join
(
' | '
)
%>
<%=
search_result
[
'
locations
'
].
join
(
' | '
)
%>
</p>
</p>
<p><strong>
Salary:
</strong>
<%=
search_result
[
'salary'
]
%>
</p>
<p><strong>
Salary:
</strong>
<%=
search_result
[
'salary'
]
%>
</p>
<p
class=
'card-text'
>
<%=
strip_tags
(
search_result
[
'description'
]).
truncate
(
Job
::
WORDS_SHORT_DESCRIPTION
)
%>
</p>
<p
class=
'card-text'
>
<%=
strip_tags
(
search_result
[
'description'
]).
truncate
(
Job
::
WORDS_SHORT_DESCRIPTION
)
%>
</p>
...
...
app/views/jobs/index.html.erb
View file @
4fa29101
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<div
class=
'content'
>
<div
class=
'content'
>
<%
if
@jobs
.
any?
%>
<%
if
@jobs
.
any?
%>
<div
class=
'message text-center'
>
<div
class=
'message text-center'
>
<h3>
We found
<%=
pluralize
(
@jobs
.
total_count
,
'result'
)
%>
for
"
<%=
@keyword
%>
"
</h3>
<h3>
We found
<%=
pluralize
(
@jobs
.
total_count
,
'result'
)
%>
for
<strong>
<%=
@keyword
%>
</strong>
</h3>
</div>
</div>
<hr>
<hr>
<%=
paginate
@jobs
%>
<%=
paginate
@jobs
%>
...
...
app/views/jobs/search.html.erb
View file @
4fa29101
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<div
class=
'content'
>
<div
class=
'content'
>
<%
if
@paginatable_array
.
any?
%>
<%
if
@paginatable_array
.
any?
%>
<div
class=
'message text-center'
>
<div
class=
'message text-center'
>
<h
3>
We found
<%=
pluralize
(
@paginatable_array
.
total_count
,
'result'
)
%>
for "
<%=
@keyword
%>
"
</h3
>
<h
4>
We found
<%=
pluralize
(
@paginatable_array
.
total_count
,
'result'
)
%>
for
<strong>
<%=
@keyword
%>
</strong></h4
>
</div>
</div>
<hr>
<hr>
<%=
paginate
@paginatable_array
%>
<%=
paginate
@paginatable_array
%>
...
...
app/views/shared/_searchbar.html.erb
View file @
4fa29101
<%=
form_tag
search_jobs_path
,
method: :get
,
class:
"mt-4 form-group
#{
my_class
}
"
,
enforce_utf8:
false
do
%>
<%=
form_tag
search_jobs_path
,
method: :get
,
class:
"mt-4 form-group
#{
my_class
}
"
,
enforce_utf8:
false
do
%>
<%=
search_field_tag
:search
,
params
[
:search
],
placeholder:
'Search'
,
class:
'form-control m-2'
%>
<%=
search_field_tag
:search
,
params
[
:search
],
placeholder:
'Search'
,
class:
'form-control m-2'
%>
<%=
select_tag
:industry
,
options_from_collection_for_select
(
@industries
,
:id
,
:name
,
'1'
)
,
class:
'form-control m-2'
%>
<%=
select_tag
:industry
,
options_from_collection_for_select
(
@industries
,
:id
,
:name
,
params
[
:industry
]),
prompt:
'All Industries'
,
class:
'form-control m-2'
%>
<%=
select_tag
:location
,
options_from_collection_for_select
(
@locations
,
:id
,
:city
,
'1'
)
,
class:
'form-control m-2'
%>
<%=
select_tag
:location
,
options_from_collection_for_select
(
@locations
,
:id
,
:city
,
params
[
:location
]),
prompt:
'All Locations'
,
class:
'form-control m-2'
%>
<%=
submit_tag
'Search'
,
name:
nil
,
class:
'btn btn-outline-success m-2'
%>
<%=
submit_tag
'Search'
,
name:
nil
,
class:
'btn btn-outline-success m-2'
%>
<%
end
%>
<%
end
%>
lib/common/solr_server.rb
0 → 100644
View file @
4fa29101
class
SolrServer
def
initialize
@solr
=
RSolr
.
connect
(
url:
'http://192.168.1.133:8983/solr/venjob'
)
end
def
search
(
keyword
,
industry_id
,
location_id
,
page
)
query
,
fq
=
set_query_search
(
keyword
,
industry_id
,
location_id
)
@solr
.
paginate
(
page
,
Job
::
NUMBER_SEARCH_RESULTS
,
'select'
,
params:
{
q:
query
,
fq:
fq
})
end
private
def
set_query_search
(
keyword
,
industry_id
,
location_id
)
industry_id
=
industry_id
.
blank?
?
"*"
:
RSolr
.
solr_escape
(
industry_id
)
location_id
=
location_id
.
blank?
?
"*"
:
RSolr
.
solr_escape
(
location_id
)
keyword
=
keyword
.
blank?
?
"*"
:
RSolr
.
solr_escape
(
keyword
)
query
=
"(title:
#{
keyword
}
)^5 OR company:
#{
keyword
}
"
fq
=
[
"industry_ids:
#{
industry_id
}
"
,
"location_ids:
#{
location_id
}
"
]
[
query
,
fq
]
end
end
lib/tasks/solr.rake
0 → 100644
View file @
4fa29101
# require './lib/common/solr_server'
# namespace :solr do
# desc 'reindex data'
# task reindex: :environment do
# solr = SolrServer.new
# solr.delete_by_query '*:*'
# jobs = Job.includes(:company, :industries, :locations)
# documents = []
# jobs.each_with_index do |job, index|
# documents[index] = {
# id: job.id,
# title: job.title,
# description: job.description,
# salary: job.salary,
# company: job.company.name,
# industry_ids: job.industries.map(&:id),
# industries: job.industries.map(&:name),
# location_ids: job.locations.map(&:id),
# locations: job.locations.map(&:city)
# }
# end
# solr.add documents, add_attributes: {commitWithin: 10}
# 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