Commit d41289032ffbd02dacbbcbb4d2ccbfe16b4b4c74

Authored by Daniel Damasceno
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>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 class ElasticsearchPluginController < ApplicationController 1 class ElasticsearchPluginController < ApplicationController
2 no_design_blocks 2 no_design_blocks
3 3
  4 + SEARCHABLE_MODELS = {communities: true, articles: true, people: true}
  5 +
4 def index 6 def index
5 search() 7 search()
6 render :action => 'search' 8 render :action => 'search'
@@ -12,11 +14,15 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -12,11 +14,15 @@ class ElasticsearchPluginController &lt; ApplicationController
12 @checkbox = {} 14 @checkbox = {}
13 15
14 if params[:model].present? 16 if params[:model].present?
15 - params[:model].keys.each do |model| 17 + params[:model].keys.each do |model|
16 @checkbox[model.to_sym] = true 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 end 26 end
21 end 27 end
22 28
@@ -37,16 +43,26 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -37,16 +43,26 @@ class ElasticsearchPluginController &lt; ApplicationController
37 end 43 end
38 44
39 query = { 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 end 67 end
52 query 68 query
@@ -64,4 +80,11 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -64,4 +80,11 @@ class ElasticsearchPluginController &lt; ApplicationController
64 end 80 end
65 terms 81 terms
66 end 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 end 90 end