elasticsearch_plugin_controller.rb
828 Bytes
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
class ElasticsearchPluginController < ApplicationController
no_design_blocks
def search
@results = []
@query = params[:q]
@checkbox = {}
if params[:model].present?
params[:model].keys.each do |model|
@checkbox[model.to_sym] = true
klass = model.classify.constantize
query = get_query params[:q], klass
@results += klass.__elasticsearch__.search(query).records.to_a
end
end
end
private
def get_query text, klass
query = {}
unless text.blank?
fields = klass::SEARCHABLE_FIELDS.map {|k, v| "#{k}^#{v[:weight]}"}
query = {
query: {
multi_match: {
query: text,
fields: fields
}
}
}
end
query
end
end