Commit ea725d57 by Tô Ngọc Ánh

search full-text v1

parent 5441a47c
......@@ -70,4 +70,5 @@ gem 'whenever', require: false
gem 'kaminari'
gem 'devise'
gem 'carrierwave', '~> 2.0'
gem 'rsolr'
##
......@@ -94,6 +94,8 @@ GEM
railties (>= 3.2)
erubi (1.9.0)
execjs (2.7.0)
faraday (1.0.1)
multipart-post (>= 1.2, < 3)
ffi (1.13.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
......@@ -135,6 +137,7 @@ GEM
mini_portile2 (2.4.0)
minitest (5.14.1)
msgpack (1.3.3)
multipart-post (2.1.1)
mysql2 (0.5.3)
nio4r (2.5.2)
nokogiri (1.10.10)
......@@ -177,6 +180,9 @@ GEM
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rsolr (2.3.0)
builder (>= 2.1.2)
faraday (>= 0.9.0)
ruby-vips (2.0.17)
ffi (~> 1.9)
ruby_dep (1.5.0)
......@@ -250,6 +256,7 @@ DEPENDENCIES
nokogiri
puma (~> 3.11)
rails (~> 5.2.4, >= 5.2.4.3)
rsolr
rubyzip
sass-rails (~> 5.0)
selenium-webdriver
......
class JobsController < ApplicationController
before_action :get_data_search_bar, only: %i[index search]
def index
@locations = Location.select(:id, :city)
@industries = Industry.select(:id, :name)
object = params[:model].classify.constantize.find_by_slug(params[:slug])
@keyword = object.try(:name) || object.try(:city)
@jobs = object.jobs.all.includes(:company, :locations).page(params[:page])
......@@ -16,10 +16,23 @@ class JobsController < ApplicationController
save_history(@job.id)
end
def search
solr = RSolr.connect(url: 'http://localhost:8983/solr/venjob')
query = params[:search].blank? ? '*:*' : "title:#{params[:search]} OR company:#{params[:search]}"
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])
@keyword = params[:search]
end
private
def save_history(job_id)
history = current_user.histories.find_or_create_by(job_id: job_id)
history.update_attributes(updated_at: Time.now)
end
def get_data_search_bar
@locations = Location.select(:id, :city)
@industries = Industry.select(:id, :name)
end
end
......@@ -7,7 +7,8 @@ class Job < ApplicationRecord
NUMBER_LATEST_JOB = 6
WORDS_SHORT_DESCRIPTION = 250
NUMBER_SEARCH_RESULTS = 20
belongs_to :company
has_many :applied_jobs
has_many :histories
......
<div class='card flex-md-row align-items-center my-2'>
<div class='card-body'>
<%= link_to search_result['title'], job_path(id: search_result['id']), class: 'card-title font-weight-bold text-decoration-none' %>
<p class='card-text'><%= search_result['company'] %></p>
<p class='mb-0'>
<strong>Work place:</strong>
<%= search_result['city'].join(' | ') %>
</p>
<p><strong>Salary: </strong><%= search_result['salary'] %></p>
<p class='card-text'><%= strip_tags(search_result['description']).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p>
</div>
<div id="favorite-<%= search_result['id'] %>" class='p-2'>
<%= render 'favorites/link_favorite', job_id: search_result['id'] %>
</div>
</div>
<div class='px-5'>
<%= render 'shared/searchbar', my_class: 'd-flex flex-column flex-md-row' %>
</div>
<div class='content'>
<% if @paginatable_array.any? %>
<div class='message text-center'>
<h3>We found <%= pluralize(@paginatable_array.total_count, 'result') %> for "<%= @keyword %>" </h3>
</div>
<hr>
<%= paginate @paginatable_array %>
<%= render partial: 'jobs/search_result', collection: @paginatable_array %>
<%= paginate @paginatable_array %>
<% else %>
<h4 class="text-center">Sorry! We can't found what you want!</h4>
<% end %>
</div>
\ No newline at end of file
<%= form_tag '#', method: :get, class: "mt-4 form-group #{my_class}" do %>
<%= search_field_tag :search, params[:keyword], placeholder: 'Search', class: 'form-control m-2' %>
<%= 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' %>
<%= select_tag :industry, options_from_collection_for_select(@industries, :id, :name, '1'), class: 'form-control m-2' %>
<%= select_tag :location, options_from_collection_for_select(@locations, :id, :city, '1'), class: 'form-control m-2' %>
<%= submit_tag 'Search', class: 'btn btn-outline-success m-2' %>
<%= submit_tag 'Search', name: nil, class: 'btn btn-outline-success m-2' %>
<% end %>
......@@ -6,6 +6,7 @@ Rails.application.routes.draw do
get 'industries', to: 'industries#index'
get 'detail/:id', to: 'jobs#show', as: :job
get 'jobs/:model/:slug', to: 'jobs#index', as: :jobs
get 'jobs', to: 'jobs#search', as: :search_jobs
get 'my', to: 'users#my_page', as: :my_page
get 'my/jobs', to: 'applied_jobs#index', as: :my_applied_jobs
......
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