Class: Noiseless::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/noiseless/mapping.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Mapping

Instance methods



86
87
88
# File 'lib/noiseless/mapping.rb', line 86

def initialize(document)
  @document = document
end

Class Method Details

.analyzer(name, definition) ⇒ Object



33
34
35
36
# File 'lib/noiseless/mapping.rb', line 33

def analyzer(name, definition)
  @analyzers ||= {}
  @analyzers[name.to_s] = definition
end

.analyzersObject



38
39
40
# File 'lib/noiseless/mapping.rb', line 38

def analyzers
  @analyzers || {}
end

.deserialize(hit) ⇒ Object



102
103
104
# File 'lib/noiseless/mapping.rb', line 102

def self.deserialize(hit)
  hit["_source"]
end

.index_name(name = nil) ⇒ Object



42
43
44
45
# File 'lib/noiseless/mapping.rb', line 42

def index_name(name = nil)
  @index_name = name.to_s if name
  @index_name
end

.inherited(subclass) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/noiseless/mapping.rb', line 6

def inherited(subclass)
  super
  # Inherit mappings and settings from parent class
  subclass.instance_variable_set(:@mapping_definition, @mapping_definition&.dup)
  subclass.instance_variable_set(:@index_settings, @index_settings&.dup)
  subclass.instance_variable_set(:@analyzers, @analyzers&.dup)
end

.load_settings_from_file(file_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/noiseless/mapping.rb', line 68

def load_settings_from_file(file_path)
  return unless File.exist?(file_path)

  content = File.read(file_path)
  parsed_settings = case File.extname(file_path)
                    when ".json"
                      JSON.parse(content)
                    when ".yml", ".yaml"
                      YAML.load(content)
                    else
                      raise ArgumentError, "Unsupported file format: #{File.extname(file_path)}"
                    end

  settings(parsed_settings)
end

.mappingObject



14
15
16
17
18
19
20
# File 'lib/noiseless/mapping.rb', line 14

def mapping(&)
  if block_given?
    @mapping_definition = MappingDefinition.new
    @mapping_definition.instance_eval(&)
  end
  @mapping_definition
end

.settings(settings_hash = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/noiseless/mapping.rb', line 22

def settings(settings_hash = nil, &)
  if settings_hash
    @index_settings = (@index_settings || {}).merge(settings_hash)
  elsif block_given?
    @index_settings ||= {}
    builder = SettingsBuilder.new(@index_settings)
    builder.instance_eval(&)
  end
  @index_settings
end

.to_mapping_hashObject



47
48
49
50
51
# File 'lib/noiseless/mapping.rb', line 47

def to_mapping_hash
  return {} unless @mapping_definition

  { properties: @mapping_definition.to_hash }
end

.to_settings_hashObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/noiseless/mapping.rb', line 53

def to_settings_hash
  settings = @index_settings || {}

  # Add analyzers to settings if any are defined
  if @analyzers&.any?
    settings = settings.deep_merge(
      analysis: {
        analyzer: @analyzers
      }
    )
  end

  settings
end

Instance Method Details

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/noiseless/mapping.rb', line 90

def to_h
  if @document.respond_to?(:to_search_document)
    @document.to_search_document
  elsif @document.respond_to?(:to_h)
    @document.to_h
  elsif @document.respond_to?(:attributes)
    @document.attributes
  else
    @document
  end
end