Class: ElasticsearchRecord::Query
- Inherits:
-
Object
- Object
- ElasticsearchRecord::Query
- Defined in:
- lib/elasticsearch_record/query.rb
Direct Known Subclasses
Constant Summary collapse
- STATUS_VALID =
STATUS CONSTANTS
:valid
- STATUS_FAILED =
:failed
- TYPE_UNDEFINED =
-- UNDEFINED TYPE ------------------------------------------------------------------------------------------------
:undefined
- TYPE_COUNT =
-- QUERY TYPES ---------------------------------------------------------------------------------------------------
:count
- TYPE_SEARCH =
:search
- TYPE_MSEARCH =
:msearch
- TYPE_SQL =
:sql
- TYPE_ESQL =
:esql
- TYPE_CREATE =
-- DOCUMENT TYPES ------------------------------------------------------------------------------------------------
:create
- TYPE_UPDATE =
:update
- TYPE_UPDATE_BY_QUERY =
:update_by_query
- TYPE_DELETE =
:delete
- TYPE_DELETE_BY_QUERY =
:delete_by_query
- TYPE_INDEX_CREATE =
-- INDEX TYPES ---------------------------------------------------------------------------------------------------
:index_create
- TYPE_INDEX_CLONE =
:index_clone
- TYPE_INDEX_UPDATE_MAPPING =
INDEX update is not implemented by Elasticsearch
- this is handled through individual updates of +mappings+, +settings+ & +aliases+. INDEX delete is handled directly as API-call
:index_update_mapping
- TYPE_INDEX_UPDATE_SETTING =
:index_update_setting
- TYPE_INDEX_UPDATE_ALIAS =
:index_update_alias
- TYPE_INDEX_DELETE_ALIAS =
:index_delete_alias
- TYPES =
includes valid types only
[ # -- QUERY TYPES TYPE_COUNT, TYPE_SEARCH, TYPE_MSEARCH, TYPE_SQL, TYPE_ESQL, # -- DOCUMENT TYPES TYPE_CREATE, TYPE_UPDATE, TYPE_UPDATE_BY_QUERY, TYPE_DELETE, TYPE_DELETE_BY_QUERY, # -- INDEX TYPES TYPE_INDEX_CREATE, TYPE_INDEX_CLONE, TYPE_INDEX_UPDATE_MAPPING, TYPE_INDEX_UPDATE_SETTING, TYPE_INDEX_UPDATE_ALIAS, TYPE_INDEX_DELETE_ALIAS ].freeze
- READ_TYPES =
includes reading types only
[ TYPE_COUNT, TYPE_SEARCH, TYPE_MSEARCH, TYPE_SQL, TYPE_ESQL ].freeze
- FAILED_BODIES =
defines a body to be executed if the query fails - +(none)+ acts like the SQL-query "where('1=0')"
{ TYPE_SEARCH => { size: 0, query: { bool: { filter: [{ term: { _id: '_' } }] } } }, TYPE_COUNT => { query: { bool: { filter: [{ term: { _id: '_' } }] } } } }.freeze
- GATES =
defines special api gates to be used per type. if no special type is defined, it simply uses +[:core,self.type]+
{ TYPE_SQL => [:sql, :query], TYPE_ESQL => [:esql, :query], TYPE_INDEX_CREATE => [:indices, :create], TYPE_INDEX_CLONE => [:indices, :clone], TYPE_INDEX_UPDATE_MAPPING => [:indices, :put_mapping], TYPE_INDEX_UPDATE_SETTING => [:indices, :put_settings], TYPE_INDEX_UPDATE_ALIAS => [:indices, :put_alias], TYPE_INDEX_DELETE_ALIAS => [:indices, :delete_alias], }.freeze
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
defines the query arguments to be passed to the API.
-
#Array ⇒ Object
defines the columns to assign from the query.
-
#Boolean ⇒ Object
defines if the affected shards gets refreshed to make this operation visible to search.
-
#columns ⇒ Object
readonly
defines the columns to assign from the query.
-
#Hash ⇒ Object
defines the query arguments to be passed to the API.
-
#index ⇒ Object
readonly
defines the index the query should be executed on.
-
#Integer|String ⇒ Object
defines the query timeout.
-
#refresh ⇒ Object
readonly
defines if the affected shards gets refreshed to make this operation visible to search.
-
#status ⇒ Object
readonly
defines the query status.
-
#String ⇒ Object
defines the index the query should be executed on.
-
#Symbol ⇒ Object
defines the query status.
-
#timeout ⇒ Object
readonly
defines the query timeout.
-
#type ⇒ Object
readonly
defines the query type.
Instance Method Summary collapse
-
#body ⇒ Hash?
returns the query body - depends on the +status+! failed queried will return the related +FAILED_BODIES+ or +{}+ as fallback.
-
#failed! ⇒ ElasticsearchRecord::Query
sets the failed status for this query.
-
#failed? ⇒ Boolean
returns true, if the query failed.
-
#gate ⇒ Array<Symbol, Symbol>
returns the API gate to be called to execute the query.
-
#initialize(index: nil, type: TYPE_UNDEFINED, status: STATUS_VALID, body: nil, refresh: nil, timeout: nil, arguments: {}, columns: []) ⇒ Query
constructor
A new instance of Query.
-
#query_arguments ⇒ Hash
(also: #to_query)
builds the final query arguments.
-
#valid? ⇒ Boolean
returns true, if the query is valid (e.g. index & type defined).
-
#write? ⇒ Boolean
returns true if this is a write query.
Constructor Details
#initialize(index: nil, type: TYPE_UNDEFINED, status: STATUS_VALID, body: nil, refresh: nil, timeout: nil, arguments: {}, columns: []) ⇒ Query
Returns a new instance of Query.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/elasticsearch_record/query.rb', line 103 def initialize(index: nil, type: TYPE_UNDEFINED, status: STATUS_VALID, body: nil, refresh: nil, timeout: nil, arguments: {}, columns: []) @index = index @type = type @status = status @refresh = refresh @timeout = timeout @body = body @arguments = arguments @columns = columns end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
defines the query arguments to be passed to the API
97 98 99 |
# File 'lib/elasticsearch_record/query.rb', line 97 def arguments @arguments end |
#Array ⇒ Object
defines the columns to assign from the query
101 |
# File 'lib/elasticsearch_record/query.rb', line 101 attr_reader :columns |
#Boolean ⇒ Object
defines if the affected shards gets refreshed to make this operation visible to search
89 |
# File 'lib/elasticsearch_record/query.rb', line 89 attr_reader :refresh |
#columns ⇒ Object (readonly)
defines the columns to assign from the query
101 102 103 |
# File 'lib/elasticsearch_record/query.rb', line 101 def columns @columns end |
#Hash ⇒ Object
defines the query arguments to be passed to the API
97 |
# File 'lib/elasticsearch_record/query.rb', line 97 attr_reader :arguments |
#index ⇒ Object (readonly)
defines the index the query should be executed on
75 76 77 |
# File 'lib/elasticsearch_record/query.rb', line 75 def index @index end |
#Integer|String ⇒ Object
defines the query timeout
93 |
# File 'lib/elasticsearch_record/query.rb', line 93 attr_reader :timeout |
#refresh ⇒ Object (readonly)
defines if the affected shards gets refreshed to make this operation visible to search
89 90 91 |
# File 'lib/elasticsearch_record/query.rb', line 89 def refresh @refresh end |
#status ⇒ Object (readonly)
defines the query status.
85 86 87 |
# File 'lib/elasticsearch_record/query.rb', line 85 def status @status end |
#String ⇒ Object
defines the index the query should be executed on
75 |
# File 'lib/elasticsearch_record/query.rb', line 75 attr_reader :index |
#Symbol ⇒ Object
defines the query status.
80 |
# File 'lib/elasticsearch_record/query.rb', line 80 attr_reader :type |
#timeout ⇒ Object (readonly)
defines the query timeout
93 94 95 |
# File 'lib/elasticsearch_record/query.rb', line 93 def timeout @timeout end |
#type ⇒ Object (readonly)
defines the query type.
80 81 82 |
# File 'lib/elasticsearch_record/query.rb', line 80 def type @type end |
Instance Method Details
#body ⇒ Hash?
returns the query body - depends on the +status+! failed queried will return the related +FAILED_BODIES+ or +{}+ as fallback
153 154 155 156 157 |
# File 'lib/elasticsearch_record/query.rb', line 153 def body return (FAILED_BODIES[self.type].presence || {}) if failed? @body end |
#failed! ⇒ ElasticsearchRecord::Query
sets the failed status for this query. returns self
117 118 119 120 121 |
# File 'lib/elasticsearch_record/query.rb', line 117 def failed! @status = STATUS_FAILED self end |
#failed? ⇒ Boolean
returns true, if the query failed
125 126 127 |
# File 'lib/elasticsearch_record/query.rb', line 125 def failed? self.status == STATUS_FAILED end |
#gate ⇒ Array<Symbol, Symbol>
returns the API gate to be called to execute the query. each query type needs a different endpoint.
140 141 142 |
# File 'lib/elasticsearch_record/query.rb', line 140 def gate GATES[self.type].presence || [:core, self.type] end |
#query_arguments ⇒ Hash Also known as: to_query
builds the final query arguments. Depends on the query status, index, body & refresh attributes. Also used possible PRE-defined arguments to be merged with those mentioned attributes.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/elasticsearch_record/query.rb', line 163 def query_arguments args = @arguments.deep_dup # set index, if present args[:index] = self.index if self.index.present? # set body, if present args[:body] = self.body if self.body.present? # set refresh, if defined (also includes false value) args[:refresh] = self.refresh unless self.refresh.nil? # set timeout, if present args[:timeout] = self.timeout if self.timeout.present? args end |
#valid? ⇒ Boolean
returns true, if the query is valid (e.g. index & type defined)
131 132 133 134 |
# File 'lib/elasticsearch_record/query.rb', line 131 def valid? # type mus be valid + index must be present (not required for SQL) TYPES.include?(self.type) #&& (index.present? || self.type == TYPE_SQL) end |
#write? ⇒ Boolean
returns true if this is a write query
146 147 148 |
# File 'lib/elasticsearch_record/query.rb', line 146 def write? !READ_TYPES.include?(self.type) end |