|
1 |
| -require 'elasticsearch/model' |
2 |
| - |
3 | 1 | class Post < ActiveRecord::Base
|
4 | 2 | 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 | + } |
38 | 14 |
|
39 | 15 | attr_reader :member_id
|
40 | 16 |
|
@@ -72,30 +48,6 @@ def self.inherited(child)
|
72 | 48 | validates :category, presence: true
|
73 | 49 | validates :title, presence: true
|
74 | 50 |
|
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 |
| - |
99 | 51 | def as_indexed_json(*)
|
100 | 52 | as_json(only: [:title, :description, :tags, :organization_id])
|
101 | 53 | end
|
|
0 commit comments