Commit 67778ac4 by nnnghia98

index database

parent 2324f19a
......@@ -21,12 +21,13 @@ gem 'jbuilder'
gem 'jquery-rails', '4.3.1'
gem 'carrierwave'
gem 'kaminari'
gem 'sunspot_rails'
gem 'sunspot_solr'
gem 'devise'
gem 'activerecord-import'
gem 'config'
gem 'draper'
gem 'rsolr'
gem 'rsolr-ext'
gem 'pry'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
......
......@@ -192,7 +192,6 @@ GEM
nokogiri (1.10.5)
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pr_geohash (1.0.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
......@@ -242,6 +241,8 @@ GEM
rsolr (2.2.1)
builder (>= 2.1.2)
faraday (>= 0.9.0)
rsolr-ext (1.0.3)
rsolr (>= 1.0.2)
ruby-vips (2.0.16)
ffi (~> 1.9)
ruby_dep (1.5.0)
......@@ -271,13 +272,6 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sunspot (2.5.0)
pr_geohash (~> 1.0)
rsolr (>= 1.1.1, < 3)
sunspot_rails (2.5.0)
rails (>= 3)
sunspot (= 2.5.0)
sunspot_solr (2.5.0)
thor (0.20.3)
thread_safe (0.3.6)
tilt (2.0.10)
......@@ -329,12 +323,12 @@ DEPENDENCIES
pry
puma (~> 3.11)
rails (~> 6.0.0)
rsolr
rsolr-ext
sass-rails (~> 5)
selenium-webdriver
spring
spring-watcher-listen (~> 2.0.0)
sunspot_rails
sunspot_solr
turbolinks (~> 5)
tzinfo-data
web-console (>= 3.3.0)
......
require "rsolr"
class SolrService
def initialize
@solr = RSolr.connect(
:url => Settings.solr.connection.server_url,
:read_timeout => Settings.solr.connection.read_timeout,
:open_timeout => Settings.solr.connection.open_timeout,
:retry_503 => Settings.solr.connection.retry_503
)
end
def add_data
jobs = Job.includes(:cities, :industries, :company).all
jobs_solr_index = jobs.map do |job|
{
id: job.id,
title: job.title,
category: job.category,
description: job.description,
short_des: job.short_des,
salary: job.salary,
company: job.company.name,
city: job.cities&.first&.name,
industry: job.industries&.first&.name
}
end
@solr.add jobs_solr_index
@solr.commit
end
def delete_data
jobs = Job.includes(:cities, :industries, :company).all
jobs_solr_delete = jobs.map do |job|
{
id: job.id
}
end
@solr.delete_by_id jobs_solr_delete
@solr.commit
end
end
......@@ -9,3 +9,11 @@ top:
limit: 9
job:
limit: 5
solr:
connection:
read_timeout: 120
open_timeout: 120
retry_503: 1
retry_after_limit: 1
server_url: "http://localhost:8983/solr/venjob"
namespace :task do
namespace :csv do
desc "import data"
task import: :environment do
Import.new.import
......@@ -7,3 +7,21 @@ namespace :task do
import.info "Cities, industries, companies, jobs imported succesfully at #{Time.current}"
end
end
namespace :solr do
desc "solr index data"
task solr_index: :environment do
SolrService.new.add_data
index = ActiveSupport::Logger.new("log/solr_service.log")
index.info "Solr index data succesfully at #{Time.current}"
end
desc "solr delete data"
task solr_delete: :environment do
SolrService.new.delete_data
delete = ActiveSupport::Logger.new("log/solr_service.log")
delete.info "Solr delete data succesfully at #{Time.current}"
end
end
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