Module: ActiveRecord::ConnectionAdapters::Elasticsearch::DatabaseStatements

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::ConnectionAdapters::ElasticsearchAdapter
Defined in:
lib/active_record/connection_adapters/elasticsearch/database_statements.rb

Overview

extend adapter with query-related statements

ORIGINAL methods untouched:

  • to_sql
  • to_sql_and_binds
  • insert
  • create
  • update
  • delete
  • arel_from_relation

SUPPORTED but not used:

  • select
  • select_all
  • select_one
  • select_value
  • select_values

UNSUPPORTED methods that will be ignored:

  • build_fixture_sql
  • build_fixture_statements
  • build_truncate_statement
  • build_truncate_statements

UNSUPPORTED methods that will fail:

  • insert_fixture
  • insert_fixtures_set
  • execute_batch
  • select_prepared
  • combine_multi_statements

Instance Method Summary collapse

Instance Method Details

#exec_delete(query, name = nil, binds = []) ⇒ Integer

Executes delete query statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed query arguments. expects a integer as return.

Returns:

  • (Integer)


86
87
88
89
90
91
92
93
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 86

def exec_delete(query, name = nil, binds = [])
  result = internal_exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('deleted', result.result) unless result.result == 'deleted'

  result.total
end

#exec_insert(query, name = nil, binds = [], _pk = nil, _sequence_name = nil, returning: nil) ⇒ ElasticsearchRecord::Result

Executes insert query statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed query arguments.



57
58
59
60
61
62
63
64
65
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 57

def exec_insert(query, name = nil, binds = [], _pk = nil, _sequence_name = nil, returning: nil)
  result = internal_exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('created', result.result) unless result.result == 'created'

  # return the result object
  result
end

#exec_update(query, name = nil, binds = []) ⇒ Integer

Executes update query statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed query arguments. expects a integer as return.

Returns:

  • (Integer)


72
73
74
75
76
77
78
79
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 72

def exec_update(query, name = nil, binds = [])
  result = internal_exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('updated', result.result) unless result.result == 'updated'

  result.total
end

#last_inserted_id(result) ⇒ Object

returns the last inserted id from the result. called through #insert



128
129
130
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 128

def last_inserted_id(result)
  result.response['_id']
end

#select_count(arel, name = "Count", async: false) ⇒ Integer

executes a count query for provided arel

Returns:

  • (Integer)


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 112

def select_count(arel, name = "Count", async: false)
  query = to_sql(arel_from_relation(arel))

  # build new count query from existing query
  query = ElasticsearchRecord::Query.new(
    index:     query.index,
    type:      ElasticsearchRecord::Query::TYPE_COUNT,
    body:      query.body,
    status:    query.status,
    arguments: query.arguments)

  internal_exec_query(query, name, async: async).response['count']
end

#select_multiple(arels, name = "Multi", async: false) ⇒ ElasticsearchRecord::Result

executes a msearch for provided arels



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 97

def select_multiple(arels, name = "Multi", async: false)
  # transform arels to query objects
  queries = arels.map { |arel| to_sql(arel_from_relation(arel)) }

  # build new msearch query
  query = ElasticsearchRecord::Query.new(
    index: queries.first&.index,
    type:  ElasticsearchRecord::Query::TYPE_MSEARCH,
    body:  queries.map { |q| { search: q.body } })

  internal_exec_query(query, name, async: async)
end

#write_query?(query) ⇒ Boolean

detects if a query is a write query. since we don't provide a simple string / hash we can now access the query-object and ask for it :)

Parameters:

Returns:

  • (Boolean)

See Also:

  • DatabaseStatements#write_query?


49
50
51
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 49

def write_query?(query)
  query.write?
end