elasticsearch_plugin_controller.rb
1.24 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
47
48
49
50
51
52
53
54
55
56
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,
operator: "and"
}
},
filter: {
term: {visible: "true"}
}
}
end
query
end
def get_terms params
terms = {}
return terms unless params[:filter].present?
params[:filter].keys.each do |model|
terms[model] = {}
params[:filter][model].keys.each do |filter|
@checkbox[filter.to_sym] = true
terms[model][params[:filter][model.to_sym][filter]] = filter
end
end
terms
end
end