Commit 2c45a91bda2d7d5c340bc894c18344f35473440c
1 parent
4b07a11b
Exists in
elasticsearch_filter
and in
1 other branch
ELasticsearch: Refacted elasticsearch controller
Adding Elasticsearch_helper Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
2 changed files
with
13 additions
and
97 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
| 1 | +require_relative '../helpers/elasticsearch_helper' | ||
| 2 | + | ||
| 1 | class ElasticsearchPluginController < ApplicationController | 3 | class ElasticsearchPluginController < ApplicationController |
| 2 | no_design_blocks | 4 | no_design_blocks |
| 3 | - | ||
| 4 | - SEARCHABLE_TYPES = { :all => { label: _("All Results")}, | ||
| 5 | - :community => { label: _("Communities")}, | ||
| 6 | - :event => { label: _("Events")}, | ||
| 7 | - :person => { label: _("People")} | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | - SEARCH_FILTERS = { :lexical => { label: _("Alphabetical Order")}, | ||
| 11 | - :recent => { label: _("More Recent Order")}, | ||
| 12 | - :access => { label: _("More accessed")} | ||
| 13 | - } | 5 | + include ElasticsearchHelper |
| 14 | 6 | ||
| 15 | def index | 7 | def index |
| 16 | search() | 8 | search() |
| @@ -24,89 +16,4 @@ class ElasticsearchPluginController < ApplicationController | @@ -24,89 +16,4 @@ class ElasticsearchPluginController < ApplicationController | ||
| 24 | process_results | 16 | process_results |
| 25 | end | 17 | end |
| 26 | 18 | ||
| 27 | - def process_results | ||
| 28 | - @query = params[:query] | ||
| 29 | - | ||
| 30 | - if @selected_type == :all | ||
| 31 | - @results = search_from_all_models | ||
| 32 | - else | ||
| 33 | - @results = search_from_model @selected_type | ||
| 34 | - end | ||
| 35 | - end | ||
| 36 | - | ||
| 37 | - private | ||
| 38 | - | ||
| 39 | - def fields_from_model | ||
| 40 | - klass::SEARCHABLE_FIELDS.map do |key, value| | ||
| 41 | - if value[:weight] | ||
| 42 | - "#{key}^#{value[:weight]}" | ||
| 43 | - else | ||
| 44 | - "#{key}" | ||
| 45 | - end | ||
| 46 | - end | ||
| 47 | - end | ||
| 48 | - | ||
| 49 | - def get_query text, klass=nil | ||
| 50 | - query = {} | ||
| 51 | - unless text.blank? | ||
| 52 | - text = text.downcase | ||
| 53 | - query = { | ||
| 54 | - query: { | ||
| 55 | - match_all: { | ||
| 56 | - } | ||
| 57 | - }, | ||
| 58 | - filter: { | ||
| 59 | - regexp: { | ||
| 60 | - name: { | ||
| 61 | - value: ".*" + text + ".*" } | ||
| 62 | - } | ||
| 63 | - }, | ||
| 64 | - suggest: { | ||
| 65 | - autocomplete: { | ||
| 66 | - text: text, | ||
| 67 | - term: { | ||
| 68 | - field: "name", | ||
| 69 | - suggest_mode: "always" | ||
| 70 | - } | ||
| 71 | - } | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - } | ||
| 75 | - end | ||
| 76 | - query | ||
| 77 | - end | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - def search_from_all_models | ||
| 81 | - models = [] | ||
| 82 | - query = get_query params[:query] | ||
| 83 | - | ||
| 84 | - SEARCHABLE_TYPES.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all } | ||
| 85 | - Elasticsearch::Model.search(query, models, size: default_per_page).page(params[:page]).records | ||
| 86 | - end | ||
| 87 | - | ||
| 88 | - def search_from_model model | ||
| 89 | - begin | ||
| 90 | - klass = model.to_s.classify.constantize | ||
| 91 | - query = get_query params[:query], klass | ||
| 92 | - klass.search(query, size: default_per_page).page(params[:page]).records | ||
| 93 | - rescue | ||
| 94 | - [] | ||
| 95 | - end | ||
| 96 | - end | ||
| 97 | - | ||
| 98 | - def define_searchable_types | ||
| 99 | - @searchable_types = SEARCHABLE_TYPES | ||
| 100 | - @selected_type = params[:selected_type].nil? ? :all : params[:selected_type].to_sym | ||
| 101 | - end | ||
| 102 | - | ||
| 103 | - def define_search_fields_types | ||
| 104 | - @search_filter_types = SEARCH_FILTERS | ||
| 105 | - @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym | ||
| 106 | - end | ||
| 107 | - | ||
| 108 | - def default_per_page | ||
| 109 | - 10 | ||
| 110 | - end | ||
| 111 | - | ||
| 112 | end | 19 | end |
plugins/elasticsearch/lib/elasticsearch_plugin/api.rb
| 1 | +require_relative '../../helpers/elasticsearch_helper' | ||
| 2 | + | ||
| 3 | + | ||
| 1 | class ElasticsearchPlugin::API < Grape::API | 4 | class ElasticsearchPlugin::API < Grape::API |
| 2 | include Api::Helpers | 5 | include Api::Helpers |
| 3 | 6 | ||
| 4 | resource :search do | 7 | resource :search do |
| 5 | get do | 8 | get do |
| 6 | - target = Person.first | ||
| 7 | present target, :with => Api::Entities::Person | 9 | present target, :with => Api::Entities::Person |
| 8 | end | 10 | end |
| 11 | + | ||
| 12 | + get 'types' do | ||
| 13 | + types = {types: ElasticsearchHelper::SEARCHABLE_TYPES.stringify_keys.keys} | ||
| 14 | + present types, with: Grape::Presenters::Presenter | ||
| 15 | + end | ||
| 16 | + | ||
| 9 | end | 17 | end |
| 18 | + | ||
| 10 | end | 19 | end |