Commit 08d9d237 by Quang Vinh Nguyen

update solr search tokenize, search_text fix string to text

parent 9f94ec6d
......@@ -24,3 +24,7 @@
# Ignore master key for decrypting credentials and more.
/config/master.key
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
......@@ -11,6 +11,8 @@ gem 'jquery-rails'
gem 'kaminari'
gem 'rsolr'
gem 'rsolr-ext'
gem 'config'
gem 'twitter-bootstrap-rails', :group => :assets
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
......
......@@ -85,10 +85,16 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
commonjs (0.2.7)
concurrent-ruby (1.0.5)
concurrent-ruby (1.0.5-java)
config (1.7.0)
activesupport (>= 3.0)
deep_merge (~> 1.2.1)
dry-validation (>= 0.10.4)
crass (1.0.4)
database_cleaner (1.7.0)
deep_merge (1.2.1)
devise (4.4.3)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
......@@ -96,6 +102,33 @@ GEM
responders
warden (~> 1.2.3)
diff-lcs (1.3)
dry-configurable (0.7.0)
concurrent-ruby (~> 1.0)
dry-container (0.6.0)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.6)
concurrent-ruby (~> 1.0)
dry-equalizer (0.2.1)
dry-inflector (0.1.2)
dry-logic (0.4.2)
dry-container (~> 0.2, >= 0.2.6)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-types (0.13.2)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.2)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 0.4, >= 0.4.2)
dry-validation (0.12.0)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (~> 0.2, >= 0.2.1)
dry-equalizer (~> 0.2)
dry-logic (~> 0.4, >= 0.4.0)
dry-types (~> 0.13.1)
erubi (1.7.1)
execjs (2.7.0)
factory_girl (4.9.0)
......@@ -139,6 +172,13 @@ GEM
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
less (2.6.0)
commonjs (~> 0.2.7)
less-rails (2.8.0)
actionpack (>= 4.0)
less (~> 2.6.0)
sprockets (> 2, < 4)
tilt
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
......@@ -296,6 +336,11 @@ GEM
turbolinks (5.1.1)
turbolinks-source (~> 5.1)
turbolinks-source (5.1.0)
twitter-bootstrap-rails (4.0.0)
actionpack (~> 5.0, >= 5.0.1)
execjs (~> 2.7)
less-rails (~> 2.8, >= 2.8.0)
railties (~> 5.0, >= 5.0.1)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2018.5)
......@@ -332,6 +377,7 @@ DEPENDENCIES
capybara (>= 2.15, < 4.0)
chromedriver-helper
coffee-rails (~> 4.2)
config
database_cleaner
devise (= 4.4.3)
factory_girl_rails (= 4.9.0)
......@@ -356,6 +402,7 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
twitter-bootstrap-rails
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
......
# frozen_string_literal: true
class JobsController < ApplicationController
# rescue_from ActiveRecord::RecordNotFound, with: :job_not_found
def index
# @jobs = Job.search(params[:search]).order('created_at DESC').page(params[:page])
@job_ids = get_jobs_rsolr(params[:search] || '*')
@jobs = Job.where(id: @job_ids).order('created_at DESC').page(params[:page])
search_str = params[:search].blank? ? '*' : params[:search]
job_found = Job.search(kw: search_str, page: params[:page].to_i)
@job_ids = job_found[:job_id]
@job_num = job_found[:num]
job_search_result = Job.where(id: @job_ids)
@jobs = Kaminari.paginate_array(job_search_result,
total_count: @job_num).
page(params[:page]).
per(Kaminari.config.default_per_page)
end
def show
@job = Job.find(params[:id])
@job = Job.find_by(id: params[:id])
unless @job.nil?
return @job
else
job_not_found(params[:id])
end
end
def city
......@@ -20,17 +35,11 @@ class JobsController < ApplicationController
def home
@jobs = Job.all.order(updated_at: :desc).take(5)
@cities = City.all.select{ |city| city.jobs.any? }.take(8)
@cities = City.all.select { |city| city.jobs.any? }.take(8)
end
def get_jobs_rsolr(title = '*')
solr = RSolr.connect url: 'http://localhost:8983/solr/gettingstarted/'
search_params = { q: "search_text:*#{title.downcase}*", rows: 5_000 }
response_solr = solr.get 'select', params: search_params
if response_solr['response']['docs'].any?
response_solr['response']['docs'].collect do |row|
row['job_id']
end
end
def job_not_found(id)
redirect_to root_path
flash[:danger] = "Couldn't find job id: #{id}"
end
end
# frozen_string_literal: true
class Job < ApplicationRecord
attr_accessor :page_per
belongs_to :company, foreign_key: 'company_id'
has_many :cities_jobs, foreign_key: 'job_id',
......@@ -33,9 +34,22 @@ class Job < ApplicationRecord
validates :condition, presence: true, length: { maximum: 1000 }
validates :link, presence: true
paginates_per 10
def self.search(search)
where("title LIKE ?", "%#{search}%")
@page_per = 10
paginates_per @page_per
def self.search(search_hash = { kw: '*', page: 0})
jobs = { job_id: [], job_num: 0 }
solr = RSolr.connect url: Settings.solr.url
search_params = { q: "search_text:#{search_hash[:kw]}",
start: search_hash[:page] * Kaminari.config.default_per_page,
rows: Kaminari.config.default_per_page }
response_solr = solr.get 'select', params: search_params
if response_solr['response']['numFound'] > 0
jobs[:num] = response_solr['response']['numFound']
response_solr['response']['docs'].collect do |row|
jobs[:job_id] << row['job_id']
end
end
jobs
end
end
<li>
<div class="card" style="width: 40rem;">
<div class="card-body">
<h5 class="card-title"><%= job.title %></h5>
......@@ -8,4 +7,4 @@
<%= link_to 'Show job', job, class: 'card-link' %>
</div>
</div>
</li>
<br>
<h3>Search result: <%= pluralize(@job_ids.length, 'job') unless @job_ids.blank?%></h3>
<h3>Search result: <%= pluralize(@job_num , 'job') %></h3>
<nav class='navbar navbar-light bg-light'>
<%= form_tag(jobs_path, method: 'get',
......@@ -11,7 +11,5 @@
<% end %>
</nav>
<ul class='jobs'>
<%= paginate @jobs, outer_window: 3 %>
<%= render @jobs %>
</ul>
\ No newline at end of file
<%= paginate @jobs%>
<%= render @jobs %>
\ No newline at end of file
<li class="page-item">
<%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, remote: remote, class: 'page-link' %>
</li>
<li class='page-item disabled'>
<%= link_to raw(t 'views.pagination.truncate'), '#', class: 'page-link' %>
</li>
<li class="page-item">
<%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, remote: remote, class: 'page-link' %>
</li>
<li class="page-item">
<%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote, class: 'page-link' %>
</li>
<% if page.current? %>
<li class="page-item active">
<%= content_tag :a, page, data: { remote: remote }, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)), class: 'page-link' %>
</li>
<% else %>
<li class="page-item">
<%= link_to page, url, remote: remote, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)), class: 'page-link' %>
</li>
<% end %>
<%= paginator.render do %>
<nav>
<ul class="pagination">
<%= first_page_tag unless current_page.first? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| %>
<% if page.left_outer? || page.right_outer? || page.inside_window? %>
<%= page_tag page %>
<% elsif !page.was_truncated? -%>
<%= gap_tag %>
<% end %>
<% end %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
</ul>
</nav>
<% end %>
<li class="page-item">
<%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, rel: 'prev', remote: remote, class: 'page-link' %>
</li>
......@@ -15,8 +15,8 @@ Rails.application.configure do
# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
# config.action_controller.perform_caching = true
config.action_controller.perform_caching = false
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
......
Config.setup do |config|
# Name of the constant exposing loaded settings
config.const_name = 'Settings'
# Ability to remove elements of the array set in earlier loaded settings file. For example value: '--'.
#
# config.knockout_prefix = nil
# Overwrite an existing value when merging a `nil` value.
# When set to `false`, the existing value is retained after merge.
#
# config.merge_nil_values = true
# Overwrite arrays found in previously loaded settings file. When set to `false`, arrays will be merged.
#
# config.overwrite_arrays = true
# Load environment variables from the `ENV` object and override any settings defined in files.
#
# config.use_env = false
# Define ENV variable prefix deciding which variables to load into config.
#
# config.env_prefix = 'Settings'
# What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well
# with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where
# using dots in variable names might not be allowed (eg. Bash).
#
# config.env_separator = '.'
# Ability to process variables names:
# * nil - no change
# * :downcase - convert to lower case
#
# config.env_converter = :downcase
# Parse numeric values as integers instead of strings.
#
# config.env_parse_values = true
# Validate presence and type of specific config values. Check https://github.com/dry-rb/dry-validation for details.
#
# config.schema do
# required(:name).filled
# required(:age).maybe(:int?)
# required(:email).filled(format?: EMAIL_REGEX)
# end
end
# frozen_string_literal: true
Kaminari.configure do |config|
# config.default_per_page = 25
config.default_per_page = 10
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.window = 3
# config.outer_window = 3
# config.left = 0
# config.right = 0
# config.page_method_name = :page
......
solr:
url: "http://localhost:8983/solr/gettingstarted/"
row: 25
df: "search_text"
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