Skip to content

Commit 0fef71d

Browse files
authored
Merge pull request #568 from coopdevs/remove_elastic_2
Remove Elastic, 2nd attemp
2 parents 80652f7 + f1a0c92 commit 0fef71d

15 files changed

+1299
-366
lines changed

.travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dist: bionic
33
cache: bundler
44
services:
55
- postgresql
6-
- elasticsearch
76
addons:
87
postgresql: "9.4"
98
chrome: stable
@@ -19,7 +18,5 @@ before_script:
1918
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
2019
- chmod +x ./cc-test-reporter
2120
- ./cc-test-reporter before-build
22-
# allow elasticsearch to be ready - https://docs.travis-ci.com/user/database-setup/#ElasticSearch
23-
- sleep 10
2421
after_script:
2522
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

Gemfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ gem 'unicorn'
1616
gem 'kaminari', '~> 1.1.1'
1717
gem "simple_form", ">= 3.0.0"
1818
gem 'rollbar', '2.8.3'
19+
gem 'pg_search', '2.1.4'
1920
gem 'prawn', '~> 2.2.0'
2021
gem 'prawn-table', '~> 0.2.2'
21-
gem 'elasticsearch-model'
22-
gem 'elasticsearch-rails'
2322
gem 'skylight'
2423
gem 'sidekiq', '5.1.3'
2524
gem 'sidekiq-cron', '~> 1.1.0'

Gemfile.lock

+4-19
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,6 @@ GEM
118118
dotenv-rails (2.7.1)
119119
dotenv (= 2.7.1)
120120
railties (>= 3.2, < 6.1)
121-
elasticsearch (1.0.8)
122-
elasticsearch-api (= 1.0.7)
123-
elasticsearch-transport (= 1.0.7)
124-
elasticsearch-api (1.0.7)
125-
multi_json
126-
elasticsearch-model (0.1.7)
127-
activesupport (> 3)
128-
elasticsearch (> 0.4)
129-
hashie
130-
elasticsearch-rails (0.1.7)
131-
elasticsearch-transport (1.0.7)
132-
faraday
133-
multi_json
134121
erubi (1.9.0)
135122
erubis (2.7.0)
136123
et-orbi (1.1.7)
@@ -139,8 +126,6 @@ GEM
139126
fabrication (2.20.1)
140127
faker (1.9.6)
141128
i18n (>= 0.7)
142-
faraday (0.9.1)
143-
multipart-post (>= 1.2, < 3)
144129
ffi (1.12.2)
145130
formtastic (3.1.5)
146131
actionpack (>= 3.2.13)
@@ -154,7 +139,6 @@ GEM
154139
has_scope (0.6.0)
155140
actionpack (>= 3.2, < 5)
156141
activesupport (>= 3.2, < 5)
157-
hashie (3.4.1)
158142
hstore_translate (1.0.0)
159143
activerecord (>= 3.1.0)
160144
http-cookie (1.0.3)
@@ -207,7 +191,6 @@ GEM
207191
mini_portile2 (2.4.0)
208192
minitest (5.14.0)
209193
multi_json (1.11.2)
210-
multipart-post (2.0.0)
211194
net-scp (2.0.0)
212195
net-ssh (>= 2.6.5, < 6.0.0)
213196
net-ssh (5.2.0)
@@ -220,6 +203,9 @@ GEM
220203
ast (~> 2.4.0)
221204
pdf-core (0.7.0)
222205
pg (0.21.0)
206+
pg_search (2.1.4)
207+
activerecord (>= 4.2)
208+
activesupport (>= 4.2)
223209
prawn (2.2.2)
224210
pdf-core (~> 0.7.0)
225211
ttfunk (~> 1.5)
@@ -411,8 +397,6 @@ DEPENDENCIES
411397
database_cleaner (= 1.6.2)
412398
devise (~> 4.7.1)
413399
dotenv-rails (~> 2.7.1)
414-
elasticsearch-model
415-
elasticsearch-rails
416400
fabrication (~> 2.20)
417401
faker (~> 1.9)
418402
has_scope
@@ -423,6 +407,7 @@ DEPENDENCIES
423407
letter_opener (= 1.4.1)
424408
localeapp (= 2.1.1)
425409
pg (= 0.21.0)
410+
pg_search (= 2.1.4)
426411
prawn (~> 2.2.0)
427412
prawn-table (~> 0.2.2)
428413
pundit (~> 2.0.0)

app/controllers/members_controller.rb

-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ def toggle_manager
2020
def toggle_active
2121
find_member
2222
@member.toggle(:active).save!
23-
if @member.active
24-
@member.add_all_posts_to_index
25-
else
26-
@member.remove_all_posts_from_index
27-
end
2823
respond_to do |format|
2924
format.json { head :ok }
3025
format.html { redirect_to :back }

app/controllers/posts_controller.rb

+15-24
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,22 @@ class PostsController < ApplicationController
44
has_scope :by_organization, as: :org
55

66
def index
7-
if (query = params[:q]).present?
8-
# match query term on fields
9-
must = [ { multi_match: {
10-
query: query.to_s,
11-
type: "phrase_prefix",
12-
fields: ["title^2", "description", "tags^2"]
13-
} } ]
14-
if current_organization.present?
15-
# filter by organization
16-
must << { term: { organization_id: { value: current_organization.id } } }
17-
end
18-
posts = model.__elasticsearch__.search(
19-
query: {
20-
bool: {
21-
must: must
22-
}
23-
}
24-
).page(params[:page]).per(25).records
25-
else
26-
posts = model.active.of_active_members
27-
if current_organization.present?
28-
posts = posts.merge(current_organization.posts)
29-
end
30-
posts = apply_scopes(posts).page(params[:page]).per(25)
7+
context = model.active.of_active_members
8+
if current_organization.present?
9+
context = context.where(
10+
organization_id: current_organization.id
11+
)
3112
end
13+
14+
posts = if (query = params[:q]).present?
15+
context.
16+
search_by_query(query).
17+
page(params[:page]).
18+
per(25)
19+
else
20+
apply_scopes(context).page(params[:page]).per(25)
21+
end
22+
3223
instance_variable_set("@#{resources}", posts)
3324
end
3425

app/models/member.rb

-12
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ def display_id
4444
member_uid
4545
end
4646

47-
def remove_all_posts_from_index
48-
Post.with_member.where("members.id = ?", self.id).find_each do |post|
49-
post.delete_document
50-
end
51-
end
52-
53-
def add_all_posts_to_index
54-
Post.with_member.where("members.id = ?", self.id).find_each do |post|
55-
post.update_or_delete_document(self)
56-
end
57-
end
58-
5947
def assign_registration_number
6048
self.member_uid ||= organization.next_reg_number_seq
6149
end

app/models/post.rb

+11-59
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
1-
require 'elasticsearch/model'
2-
31
class Post < ActiveRecord::Base
42
include Taggable
5-
6-
# Elasticsearch::Model doesn't work well with STI, so
7-
# include it in subclasses directly.
8-
def self.inherited(child)
9-
super
10-
11-
child.instance_eval do
12-
include Elasticsearch::Model
13-
14-
after_commit :index_document, on: :create
15-
after_commit :update_or_delete_document, on: :update
16-
after_commit :delete_document, on: :destroy
17-
18-
settings(
19-
analysis: {
20-
analyzer: {
21-
normal: {
22-
tokenizer: "standard",
23-
# lowercase, unaccent
24-
filter: %w[lowercase asciifolding]
25-
}
26-
}
27-
}
28-
) do
29-
mapping do
30-
indexes :title, analyzer: "normal"
31-
indexes :description, analyzer: "normal"
32-
indexes :tags
33-
indexes :organization_id, type: :integer
34-
end
35-
end
36-
end
37-
end
3+
include PgSearch
4+
5+
pg_search_scope :search_by_query,
6+
against: [:title, :description, :tags],
7+
ignoring: :accents,
8+
using: {
9+
tsearch: {
10+
prefix: true,
11+
tsvector_column: 'tsv'
12+
}
13+
}
3814

3915
attr_reader :member_id
4016

@@ -72,30 +48,6 @@ def self.inherited(child)
7248
validates :category, presence: true
7349
validates :title, presence: true
7450

75-
def index_document
76-
__elasticsearch__.index_document
77-
end
78-
79-
# pass member when doing bulk things
80-
def update_or_delete_document(member = nil)
81-
member ||= self.member
82-
if active && member.try(:active)
83-
begin
84-
__elasticsearch__.update_document
85-
rescue # document was not in the index. TODO: more specifi exception class
86-
__elasticsearch__.index_document
87-
end
88-
else
89-
__elasticsearch__.delete_document
90-
end
91-
rescue # document was not in the index. TODO: more specifi exception class
92-
end
93-
94-
def delete_document
95-
__elasticsearch__.delete_document
96-
rescue # document was not in the index. TODO: more specifi exception class
97-
end
98-
9951
def as_indexed_json(*)
10052
as_json(only: [:title, :description, :tags, :organization_id])
10153
end

config/application.rb

+4
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ class Application < Rails::Application
3535

3636
# ActiveJob configuration
3737
config.active_job.queue_adapter = :sidekiq
38+
39+
# Use db/structure.sql with SQL as schema format
40+
# This is needed to store in the schema SQL statements not covered by the ORM
41+
config.active_record.schema_format = :sql
3842
end
3943
end

config/initializers/elasticsearch.rb

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class AddTsvectorColumnToPost < ActiveRecord::Migration
2+
def up
3+
execute <<-SQL
4+
ALTER TABLE posts ADD COLUMN tsv tsvector;
5+
6+
CREATE FUNCTION posts_trigger() RETURNS trigger AS $$
7+
begin
8+
new.tsv :=
9+
to_tsvector('simple', unaccent(coalesce(new.title::text, ''))) ||
10+
to_tsvector('simple', unaccent(coalesce(new.description::text, ''))) ||
11+
to_tsvector('simple', unaccent(coalesce(new.tags::text, '')));
12+
return new;
13+
end
14+
$$ LANGUAGE plpgsql;
15+
16+
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
17+
ON posts FOR EACH ROW EXECUTE PROCEDURE posts_trigger();
18+
SQL
19+
20+
add_index :posts, :tsv, using: "gin"
21+
end
22+
23+
def down
24+
execute <<-SQL
25+
DROP TRIGGER tsvectorupdate ON posts;
26+
DROP FUNCTION posts_trigger();
27+
SQL
28+
29+
remove_index :posts, :tsv
30+
remove_column :posts, :tsv
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class EnableUnaccentExtension < ActiveRecord::Migration
2+
def up
3+
enable_extension "unaccent"
4+
end
5+
6+
def down
7+
disable_extension "unaccent"
8+
end
9+
end

0 commit comments

Comments
 (0)