Class: AmberODM::Document
- Inherits:
-
AttrInitializer
- Object
- AttrInitializer
- AmberODM::Document
- Defined in:
- lib/amber_odm.rb
Instance Attribute Summary collapse
-
#_id ⇒ Object
readonly
Returns the value of attribute _id.
-
#_primary_term ⇒ Object
readonly
Returns the value of attribute _primary_term.
-
#_score ⇒ Object
readonly
Returns the value of attribute _score.
-
#_seq_no ⇒ Object
readonly
Returns the value of attribute _seq_no.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Attributes inherited from AttrInitializer
Class Method Summary collapse
-
.client ⇒ Elasticsearch::Client
The ES client.
- .db_name ⇒ Symbol?
- .index_name ⇒ Symbol, ...
- .is_valid_fields?(query_fields) ⇒ Boolean
- .search(filter, _source: [], sort: {}, search_after: [], size: 0) ⇒ Object
- .use_seq_verification ⇒ Object
- .validate_fields(fields_to_validate) ⇒ Object
Instance Method Summary collapse
- #get_bulk_update_hash(*fields) ⇒ Object
-
#initialize(document) ⇒ Document
constructor
A new instance of Document.
-
#search(query, _source: [], sort: [], search_after: [], size: 0) ⇒ Array<self>
The documents.
Methods inherited from AttrInitializer
array_to_h, fields, #nil?, #to_h
Constructor Details
#initialize(document) ⇒ Document
Returns a new instance of Document.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/amber_odm.rb', line 95 def initialize(document) @_id = document&.dig('_id').dup @_score = document&.dig('_score').dup @sort = document&.dig('sort').dup @_seq_no = document&.dig('_seq_no').dup @_primary_term = document&.dig('_primary_term').dup fields = self.class.fields fields.each do |field| if ['_id', '_score', 'sort', '_seq_no', '_primary_term'].include?(field) raise Exceptions::ReservedField.new("Field #{field} is reserved, remove it from the fields list") end document_value = document&.dig('_source')&.dig(field).dup send("#{field}=", document_value) end instance_variable_set('@_document', document) end |
Instance Attribute Details
#_id ⇒ Object (readonly)
Returns the value of attribute _id.
93 94 95 |
# File 'lib/amber_odm.rb', line 93 def _id @_id end |
#_primary_term ⇒ Object (readonly)
Returns the value of attribute _primary_term.
93 94 95 |
# File 'lib/amber_odm.rb', line 93 def _primary_term @_primary_term end |
#_score ⇒ Object (readonly)
Returns the value of attribute _score.
93 94 95 |
# File 'lib/amber_odm.rb', line 93 def _score @_score end |
#_seq_no ⇒ Object (readonly)
Returns the value of attribute _seq_no.
93 94 95 |
# File 'lib/amber_odm.rb', line 93 def _seq_no @_seq_no end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
93 94 95 |
# File 'lib/amber_odm.rb', line 93 def sort @sort end |
Class Method Details
.client ⇒ Elasticsearch::Client
Returns The ES client.
127 128 129 |
# File 'lib/amber_odm.rb', line 127 def self.client Connector.database(db_name&.to_sym) end |
.db_name ⇒ Symbol?
113 114 115 |
# File 'lib/amber_odm.rb', line 113 def self.db_name nil end |
.index_name ⇒ Symbol, ...
118 119 120 |
# File 'lib/amber_odm.rb', line 118 def self.index_name nil end |
.is_valid_fields?(query_fields) ⇒ Boolean
131 132 133 |
# File 'lib/amber_odm.rb', line 131 def self.is_valid_fields?(query_fields) (query_fields - fields).count == 0 end |
.search(filter, _source: [], sort: {}, search_after: [], size: 0) ⇒ Object
159 160 161 |
# File 'lib/amber_odm.rb', line 159 def self.search(filter, _source: [], sort: {}, search_after: [], size: 0) new({}).search(filter, _source: _source, sort: sort, search_after: search_after, size: size) end |
.use_seq_verification ⇒ Object
122 123 124 |
# File 'lib/amber_odm.rb', line 122 def self.use_seq_verification true end |
.validate_fields(fields_to_validate) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/amber_odm.rb', line 163 def self.validate_fields(fields_to_validate) fields_to_validate.map! { |field| field.to_s } if (fields_to_validate - fields).count > 0 raise Exceptions::UnknownWriteFieldException.new "Unknown fields: #{fields_to_validate - fields}, define them in attr_accessor before using them" elsif fields_to_validate.empty? raise Exceptions::IllegalArgumentException.new 'Empty fields' end end |
Instance Method Details
#get_bulk_update_hash(*fields) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/amber_odm.rb', line 172 def get_bulk_update_hash(*fields) self.class.validate_fields(fields) aggregation_hash = {} fields.each do |field| value = send(field) if value.is_a?(AttrInitializer) aggregation_hash[field] = value.to_h elsif value.is_a?(Array) aggregation_hash[field] = AttrInitializer.array_to_h(value) else aggregation_hash[field] = value end end update_hash = { update: { _index: self.class.index_name&.to_s, _id: _id, data: { doc: aggregation_hash } } } if self.class.use_seq_verification update_hash[:update][:if_seq_no] = _seq_no update_hash[:update][:if_primary_term] = _primary_term end update_hash end |
#search(query, _source: [], sort: [], search_after: [], size: 0) ⇒ Array<self>
Returns The documents.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/amber_odm.rb', line 140 def search(query, _source: [], sort: [], search_after: [], size: 0) # self.class.validate_fields_from_stages(query, _source, sort) if _source.empty? _source = self.class.fields end if query.empty? raise Exceptions::IllegalArgumentException.new end body = { _source: _source, query: query } body[:seq_no_primary_term] = true if self.class.use_seq_verification body[:sort] = sort unless sort.empty? body[:search_after] = search_after unless search_after.empty? body[:size] = size unless size.zero? response = self.class.client.search(index: self.class.index_name.to_s, body: body) response&.dig('hits', 'hits')&.map { |document| self.class.new(document) } || [] end |