community.rb
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require_dependency 'community'
require 'elasticsearch/model'
Community.class_eval do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['name', 'identifier']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
name: {},
identifier: {}
}
}
}
)
end
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :name, analyzer: 'english', index_options: 'offsets'
indexes :identifier, analyzer: 'english'
end
end
end
Community.__elasticsearch__.client.indices.delete index: Community.index_name rescue nil
# Create the new index with the new mapping
Community.__elasticsearch__.client.indices.create \
index: Community.index_name,
body: { settings: Community.settings.to_hash, mappings: Community.mappings.to_hash }
# Index all Community records from the DB to Elasticsearch
Community.import