Commit b387b21dca58d5c20c99a7113e44fc9d04ced9c9

Authored by Daniel Damasceno
Committed by Macartur Sousa
1 parent 9b48d63d

Elasticsearch: Adding searchable models

Adding community.rb and fixing searchable_fields
Adding regex filter
Add person & gallery to allow search on those models
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 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  
... ... @@ -28,25 +34,35 @@ class ElasticsearchPluginController &lt; ApplicationController
28 34 query = {}
29 35 unless text.blank?
30 36  
31   - fields = klass.indexable_fields.map do |key, value|
  37 + fields = klass::SEARCHABLE_FIELDS.map do |key, value|
32 38 if value[:weight]
33   - "#{k}^#{v[:weight]}"
  39 + "#{key}^#{value[:weight]}"
34 40 else
35   - "#{k}"
  41 + "#{key}"
36 42 end
37 43 end
38 44  
39 45 query = {
40   - query: {
41   - multi_match: {
42   - query: text,
43   - fields: fields,
44   - operator: "and"
45   - }
46   - },
47   - filter: {
48   - term: {visible: "true"}
  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"
49 62 }
  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/lib/ext/community.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +require_dependency 'community'
  2 +require_relative '../elasticsearch_indexed_model'
  3 +
  4 +class Community
  5 + include ElasticsearchIndexedModel
  6 +
  7 + def self.control_fields
  8 + []
  9 + end
  10 +end
... ...
plugins/elasticsearch/lib/ext/gallery.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +require_dependency 'gallery'
  2 +require_relative '../elasticsearch_indexed_model'
  3 +
  4 +class Gallery
  5 + include ElasticsearchIndexedModel
  6 +
  7 + def self.control_fields
  8 + [
  9 + :advertise,
  10 + :published,
  11 + ]
  12 + end
  13 +end
... ...
plugins/elasticsearch/lib/ext/person.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +require_dependency 'person'
  2 +require_relative '../elasticsearch_indexed_model'
  3 +
  4 +class Person
  5 + include ElasticsearchIndexedModel
  6 +
  7 + def self.control_fields
  8 + [
  9 + :visible,
  10 + :public_profile,
  11 + ]
  12 + end
  13 +end
... ...