Commit d41289032ffbd02dacbbcbb4d2ccbfe16b4b4c74
1 parent
0486a0a0
Exists in
elasticsearch_api
Show all results when query is empty
Signed-off-by: Daniel Henrique <danielhmarinho@gmail.com> Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Showing
1 changed file
with
35 additions
and
12 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 | 1 | class ElasticsearchPluginController < ApplicationController |
2 | 2 | no_design_blocks |
3 | 3 | |
4 | + SEARCHABLE_MODELS = {communities: true, articles: true, people: true} | |
5 | + | |
4 | 6 | def index |
5 | 7 | search() |
6 | 8 | render :action => 'search' |
... | ... | @@ -12,11 +14,15 @@ class ElasticsearchPluginController < ApplicationController |
12 | 14 | @checkbox = {} |
13 | 15 | |
14 | 16 | if params[:model].present? |
15 | - params[:model].keys.each do |model| | |
17 | + params[:model].keys.each do |model| | |
16 | 18 | @checkbox[model.to_sym] = true |
17 | - klass = model.classify.constantize | |
18 | - query = get_query params[:q], klass | |
19 | - @results |= klass.__elasticsearch__.search(query).records.to_a | |
19 | + results model | |
20 | + end | |
21 | + else | |
22 | + unless params[:q].blank? | |
23 | + SEARCHABLE_MODELS.keys.each do |model| | |
24 | + results model | |
25 | + end | |
20 | 26 | end |
21 | 27 | end |
22 | 28 | |
... | ... | @@ -37,16 +43,26 @@ class ElasticsearchPluginController < ApplicationController |
37 | 43 | end |
38 | 44 | |
39 | 45 | query = { |
40 | - query: { | |
41 | - match_all: { | |
42 | - } | |
43 | - }, | |
44 | - filter: { | |
45 | - regexp: { | |
46 | - name: { | |
47 | - value: ".*" + text + ".*" } | |
46 | + query: { | |
47 | + match_all: { | |
48 | + } | |
49 | + }, | |
50 | + filter: { | |
51 | + regexp: { | |
52 | + name: { | |
53 | + value: ".*" + text + ".*" } | |
54 | + } | |
55 | + }, | |
56 | + suggest: { | |
57 | + autocomplete: { | |
58 | + text: text, | |
59 | + term: { | |
60 | + field: "name", | |
61 | + suggest_mode: "always" | |
48 | 62 | } |
49 | 63 | } |
64 | + } | |
65 | + | |
50 | 66 | } |
51 | 67 | end |
52 | 68 | query |
... | ... | @@ -64,4 +80,11 @@ class ElasticsearchPluginController < ApplicationController |
64 | 80 | end |
65 | 81 | terms |
66 | 82 | end |
83 | + | |
84 | + def results model | |
85 | + klass = model.to_s.classify.constantize | |
86 | + query = get_query params[:q], klass | |
87 | + @results |= klass.__elasticsearch__.search(query).records.to_a | |
88 | + end | |
89 | + | |
67 | 90 | end | ... | ... |