Compare View

switch
from
...
to
 
Commits (2)
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 &lt; 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 &lt; 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 &lt; 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
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... ... @@ -15,8 +15,6 @@
15 15 <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %>
16 16 <%= label_tag('articles', _("articles")) %>
17 17  
18   - <%= check_box_tag 'model[gallery]', :type, @checkbox[:gallery] %>
19   - <%= label_tag('gallery', _("gallery")) %>
20 18 <% end %>
21 19  
22 20 <% for result in @results %>
... ...