Class: Workarea::Search::Settings

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/search/settings.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.currentSearch::Settings

The site-specific Workarea::Search::Settings to use for the current request. Thread-safe.

Returns:



21
22
23
24
# File 'app/models/workarea/search/settings.rb', line 21

def self.current
  Thread.current[:current_search_settings] ||
    find_or_create_by(index: Elasticsearch::Document.current_index_prefix)
end

.current=(settings) ⇒ Object



26
27
28
# File 'app/models/workarea/search/settings.rb', line 26

def self.current=(settings)
  Thread.current[:current_search_settings] = settings
end

Instance Method Details

#elasticsearch_settingsHash

The settings for creation of Elasticsearch indexes. Using them from here adds the synonyms as defined by the admin.

Returns:

  • (Hash)


56
57
58
59
60
61
62
63
64
65
# File 'app/models/workarea/search/settings.rb', line 56

def elasticsearch_settings
  result = Workarea.config.elasticsearch_settings.deep_dup

  result[:analysis][:filter][:synonym] = {
    type: 'synonym',
    synonyms: sanitized_synonyms
  }

  result
end

#nameString

For compatibility with admin features, models must respond to this method.

Returns:



34
35
36
# File 'app/models/workarea/search/settings.rb', line 34

def name
  'Search Settings'
end

#sanitized_synonymsObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/workarea/search/settings.rb', line 38

def sanitized_synonyms
  result = synonyms.to_s.split(/\r?\n/)

  # empty synonym filter breaks everything!
  if result.blank?
    ['synonims, synonyms']
  else
    result.map do |line|
      line.gsub(/[-–]/, ' ').gsub(/\s?,\s?/, ',').gsub(/(\A\s|\s\z)/, '').downcase
    end
  end
end