Class: ElasticsearchRecord::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch_record/query.rb

Direct Known Subclasses

Arel::Collectors::ElasticsearchQuery

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

Instance Method Summary collapse

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

#argumentsObject (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

#ArrayObject

defines the columns to assign from the query



101
# File 'lib/elasticsearch_record/query.rb', line 101

attr_reader :columns

#BooleanObject

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

#columnsObject (readonly)

defines the columns to assign from the query



101
102
103
# File 'lib/elasticsearch_record/query.rb', line 101

def columns
  @columns
end

#HashObject

defines the query arguments to be passed to the API



97
# File 'lib/elasticsearch_record/query.rb', line 97

attr_reader :arguments

#indexObject (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|StringObject

defines the query timeout



93
# File 'lib/elasticsearch_record/query.rb', line 93

attr_reader :timeout

#refreshObject (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

#statusObject (readonly)

defines the query status.

See Also:

  • STATUSES


85
86
87
# File 'lib/elasticsearch_record/query.rb', line 85

def status
  @status
end

#StringObject

defines the index the query should be executed on



75
# File 'lib/elasticsearch_record/query.rb', line 75

attr_reader :index

#SymbolObject

defines the query status.

See Also:

  • STATUSES


80
# File 'lib/elasticsearch_record/query.rb', line 80

attr_reader :type

#timeoutObject (readonly)

defines the query timeout



93
94
95
# File 'lib/elasticsearch_record/query.rb', line 93

def timeout
  @timeout
end

#typeObject (readonly)

defines the query type.

See Also:



80
81
82
# File 'lib/elasticsearch_record/query.rb', line 80

def type
  @type
end

Instance Method Details

#bodyHash?

returns the query body - depends on the +status+! failed queried will return the related +FAILED_BODIES+ or +{}+ as fallback

Returns:



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

Returns:



125
126
127
# File 'lib/elasticsearch_record/query.rb', line 125

def failed?
  self.status == STATUS_FAILED
end

#gateArray<Symbol, Symbol>

returns the API gate to be called to execute the query. each query type needs a different endpoint.

Returns:

See Also:

  • Elasticsearch::API


140
141
142
# File 'lib/elasticsearch_record/query.rb', line 140

def gate
  GATES[self.type].presence || [:core, self.type]
end

#query_argumentsHash 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.

Returns:



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)

Returns:



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

Returns:



146
147
148
# File 'lib/elasticsearch_record/query.rb', line 146

def write?
  !READ_TYPES.include?(self.type)
end