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_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,
  # -- 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
].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_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, arguments: {}, columns: []) ⇒ Query

Returns a new instance of Query.



101
102
103
104
105
106
107
108
109
# File 'lib/elasticsearch_record/query.rb', line 101

def initialize(index: nil, type: TYPE_UNDEFINED, status: STATUS_VALID, body: nil, refresh: nil, arguments: {}, columns: [])
  @index     = index
  @type      = type
  @status    = status
  @refresh   = refresh
  @body      = body
  @arguments = arguments
  @columns   = columns
end

Instance Attribute Details

#argumentsObject (readonly)

defines the query arguments to be passed to the API



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

def arguments
  @arguments
end

#ArrayObject

defines the columns to assign from the query



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

attr_reader :columns

#BooleanObject

defines if the affected shards gets refreshed to make this operation visible to search



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

attr_reader :refresh

#columnsObject (readonly)

defines the columns to assign from the query



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

def columns
  @columns
end

#HashObject

defines the query arguments to be passed to the API



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

#indexObject (readonly)

defines the index the query should be executed on



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

def index
  @index
end

#refreshObject (readonly)

defines if the affected shards gets refreshed to make this operation visible to search



87
88
89
# File 'lib/elasticsearch_record/query.rb', line 87

def refresh
  @refresh
end

#statusObject (readonly)

defines the query status.

See Also:

  • STATUSES


83
84
85
# File 'lib/elasticsearch_record/query.rb', line 83

def status
  @status
end

#StringObject

defines the index the query should be executed on



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

attr_reader :index

#SymbolObject

defines the query status.

See Also:

  • STATUSES


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

attr_reader :type

#typeObject (readonly)

defines the query type.

See Also:



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

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:



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

def body
  return (FAILED_BODIES[self.type].presence || {}) if self.status == STATUS_FAILED

  @body
end

#failed!ElasticsearchRecord::Query

sets the failed status for this query. returns self



114
115
116
117
118
# File 'lib/elasticsearch_record/query.rb', line 114

def failed!
  @status = STATUS_FAILED

  self
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


131
132
133
# File 'lib/elasticsearch_record/query.rb', line 131

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:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/elasticsearch_record/query.rb', line 154

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?

  args
end

#valid?Boolean

returns true, if the query is valid (e.g. index & type defined)

Returns:



122
123
124
125
# File 'lib/elasticsearch_record/query.rb', line 122

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:



137
138
139
# File 'lib/elasticsearch_record/query.rb', line 137

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