Module: Chewy::Index::Mapping::ClassMethods
- Defined in:
- lib/chewy/index/mapping.rb
Instance Method Summary collapse
- 
  
    
      #agg(name, &block)  ⇒ Object 
    
    
      (also: #aggregation)
    
  
  
  
  
  
  
  
  
  
    Defines an aggregation that can be bound to a query or filter. 
- 
  
    
      #field(*args, **options, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Defines mapping field for index. 
- 
  
    
      #mappings_hash  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns compiled mappings hash for current type. 
- 
  
    
      #root(**options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Defines root object for mapping and is optional for index definition. 
- 
  
    
      #supports_outdated_sync?  ⇒ true, false 
    
    
  
  
  
  
  
  
  
  
  
    Check whether the type has outdated_sync_field defined with a simple value. 
- 
  
    
      #template(*args, **options)  ⇒ Object 
    
    
      (also: #dynamic_template)
    
  
  
  
  
  
  
  
  
  
    Defines dynamic template in mapping root objects. 
Instance Method Details
#agg(name, &block) ⇒ Object Also known as: aggregation
Defines an aggregation that can be bound to a query or filter
| 147 148 149 | # File 'lib/chewy/index/mapping.rb', line 147 def agg(name, &block) self._agg_defs = _agg_defs.merge(name => block) end | 
#field(*args, **options, &block) ⇒ Object
Defines mapping field for index
The type is optional and defaults to string if not defined:
Also, multiple fields might be defined with one call and with the same options:
The only special option in the field definition
is :value. If no :value specified then just corresponding
method will be called for the indexed object. Also
:value might be a proc or indexed object method name:
The proc evaluates inside the indexed object context if its arity is 0 and in present contexts if there is an argument:
If array was returned as value - it will be put in index as well.
Fields supports nesting in case of object field type. If
user.quiz will return an array of objects, then result index content
will be an array of hashes, if user.quiz is not a collection association
then just values hash will be put in the index.
Nested fields are composed from nested objects:
Of course it is possible to define object fields contents dynamically but make sure evaluation proc returns hash:
The special case is multi_field. If type options and block are both present field is treated as a multi-field. In that case field composition changes satisfy elasticsearch rules:
| 124 125 126 127 128 129 130 | # File 'lib/chewy/index/mapping.rb', line 124 def field(*args, **, &block) if args.size > 1 args.map { |name| field(name, **) } else (Chewy::Fields::Base.new(args.first, **), &block) end end | 
#mappings_hash ⇒ Object
Returns compiled mappings hash for current type
| 180 181 182 | # File 'lib/chewy/index/mapping.rb', line 180 def mappings_hash root.mappings_hash end | 
#root(**options) ⇒ Object
Defines root object for mapping and is optional for index
definition. Use it only if you need to pass options for root
object mapping, such as date_detection or dynamic_date_formats
| 35 36 37 38 39 40 | # File 'lib/chewy/index/mapping.rb', line 35 def root(**) self.root_object ||= Chewy::Fields::Root.new(:root, **Chewy..merge()) root_object.(**) yield if block_given? root_object end | 
#supports_outdated_sync? ⇒ true, false
Check whether the type has outdated_sync_field defined with a simple value.
| 187 188 189 190 | # File 'lib/chewy/index/mapping.rb', line 187 def supports_outdated_sync? updated_at_field = root.child_hash[outdated_sync_field] if outdated_sync_field !!updated_at_field && updated_at_field.value.nil? end | 
#template(*args, **options) ⇒ Object Also known as: dynamic_template
Defines dynamic template in mapping root objects
Name for each template is generated with the following
rule: template_#!{dynamic_templates.size + 1}.
| 173 174 175 | # File 'lib/chewy/index/mapping.rb', line 173 def template(*args, **) root.dynamic_template(*args, **) end |