Module: ElasticsearchRecord::Relation::CoreMethods
- Defined in:
- lib/elasticsearch_record/relation/core_methods.rb
Instance Method Summary collapse
- #instantiate_records(rows, &block) ⇒ Object
-
#msearch(items = nil, opts = {}) ⇒ Array, ...
Allows to execute several search operations in one request.
-
#ordered_relation ⇒ Object
overwrite original methods to provide a elasticsearch version: checks against the +#access_id_fielddata?+ to ensure the Elasticsearch Cluster allows access on the +_id+ field.
-
#resolve(name = 'Load') ⇒ ElasticsearchRecord::Result
transforms the current relation into arel, compiles it to query and executes the query.
-
#to_query ⇒ Hash
returns the query hash for the current relation.
Instance Method Details
#instantiate_records(rows, &block) ⇒ Object
4 5 6 7 8 |
# File 'lib/elasticsearch_record/relation/core_methods.rb', line 4 def instantiate_records(rows, &block) # slurp the total value from the rows (rows = ElasticsearchRecord::Result) @total = rows.is_a?(::ElasticsearchRecord::Result) ? rows.total : rows.length super end |
#msearch(items = nil, opts = {}) ⇒ Array, ...
Allows to execute several search operations in one request. executes the elasticsearch +msearch+ on the related class-index.
A optionally array of items will be each yielded with a spawn(of the current relation) and item. Each yield-return will resolve its +arel+ which will then transform to multiple queries and send in a single request.
Responses can be refined by providing a +resolve+ option, to resolve specific results from each +ElasticsearchRecord::Result+ (e.g. used to resolve 'aggregations, buckets, ...')
As a default the method returns an array of (resolved) responses in the order of the provided +values+-array. This can be transformed into a hash of keys (provided items) and values (responses) by providing the +transpose+ flag.
WARNING: if the current relation is a +NullRelation+ (#none was assigned), the method directly returns nil!
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/elasticsearch_record/relation/core_methods.rb', line 73 def msearch(items = nil, opts = {}) # prevent query on +NullRelation+! return nil if null_relation? && !opts[:keep_null_relation] # check if values are provided, if not we use the arel from the current relation-scope arels = if items.nil? [arel] else # spawn a new relation to the block and maps each arel-object items.map { |value| yield(spawn, value).arel } end # check provided resolve method responses = if opts[:resolve] klass._query_by_msearch(arels).map(&opts[:resolve].to_sym) else klass._query_by_msearch(arels) end if opts[:transpose] [items, responses].transpose.to_h else responses end end |
#ordered_relation ⇒ Object
overwrite original methods to provide a elasticsearch version: checks against the +#access_id_fielddata?+ to ensure the Elasticsearch Cluster allows access on the +_id+ field.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/elasticsearch_record/relation/core_methods.rb', line 101 def ordered_relation # resolve valid primary_key (either not the '_id' or +access_id_fielddata?+ is enabled) valid_primary_key = if primary_key != '_id' || klass.connection.access_id_fielddata? primary_key else nil end # slightly changed original methods content if order_values.empty? && (implicit_order_column || valid_primary_key) if implicit_order_column && valid_primary_key && implicit_order_column != valid_primary_key order(table[implicit_order_column].asc, table[valid_primary_key].asc) else order(table[implicit_order_column || valid_primary_key].asc) end else self end end |
#resolve(name = 'Load') ⇒ ElasticsearchRecord::Result
transforms the current relation into arel, compiles it to query and executes the query. returns the result object.
PLEASE NOTE: This makes the query +immutable+ and raises a +ActiveRecord::ImmutableRelation+ if you try to change it's values.
PLEASE NOTE: resolving records (instantiate) is never possible after calling this method!
20 21 22 23 24 |
# File 'lib/elasticsearch_record/relation/core_methods.rb', line 20 def resolve(name = 'Load') # this acts the same like +#_query_by_sql+ but we can customize the instrumentation name and # do not store the records. klass.connection.select_all(arel, "#{klass.name} #{name}") end |
#to_query ⇒ Hash
returns the query hash for the current relation
28 29 30 |
# File 'lib/elasticsearch_record/relation/core_methods.rb', line 28 def to_query to_sql.query_arguments end |