Module: Cauchy::IndexSchema::Normalization

Included in:
Cauchy::IndexSchema
Defined in:
lib/cauchy/index_schema/normalization.rb

Constant Summary collapse

IGNORE_SETTINGS =
%w[
  creation_date
  uuid
  version
]

Instance Method Summary collapse

Instance Method Details

#normalize_field(key, field) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cauchy/index_schema/normalization.rb', line 23

def normalize_field(key, field)
  if field.key?('properties')
    field['properties'] = normalize_mapping(field['properties'])
    field['type'] ||= 'object'
  end

  if field['type'] == 'date'
    field['format'] ||= 'dateOptionalTime'
  end

  if ['boolean', 'long', 'double', 'date'].include?(field['type'])
    field.delete('analyzer')
    field['index'] ||= 'not_analyzed'
  end

  if key == 'properties'
    field = normalize_mapping(field)
  else
    field = normalize_value(field)
  end

  return [key, field]
end

#normalize_mapping(hash) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/cauchy/index_schema/normalization.rb', line 13

def normalize_mapping(hash)
  hash.deep_stringify_keys.sort.map do |key, field|
    if field.is_a?(Hash)
      normalize_field(key, field)
    else
      [key, field]
    end
  end.to_h
end

#normalize_settings(hash) ⇒ Object



47
48
49
# File 'lib/cauchy/index_schema/normalization.rb', line 47

def normalize_settings(hash)
  normalize_value(hash.deep_stringify_keys.except(*IGNORE_SETTINGS))
end

#normalize_value(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/cauchy/index_schema/normalization.rb', line 51

def normalize_value(value)
  case value
  when Hash
    value.sort.map {|key, v| [key, normalize_value(v)] }.to_h
  when Numeric
    value.to_s
  else
    value
  end
end