Module: ElasticsearchRecord::Querying::ClassMethods

Defined in:
lib/elasticsearch_record/querying.rb

Constant Summary collapse

ES_QUERYING_METHODS =

define additional METHODS to be delegated to the Relation

See Also:

  • ActiveRecord::Querying::QUERYING_METHODS
[
  :query,
  :filter,
  :must,
  :must_not,
  :should,
  :aggregate,
  :msearch
].freeze

Instance Method Summary collapse

Instance Method Details

#_query_by_msearch(queries, async: false) ⇒ Object

execute query by msearch



144
145
146
# File 'lib/elasticsearch_record/querying.rb', line 144

def _query_by_msearch(queries, async: false)
  connection.select_multiple(queries, "#{name} Msearch", async: async)
end

#esql(esql, async: false) ⇒ Object

executes a +esql+ by provided ES|SL query Does NOT instantiate records.

Parameters:

  • esql (String)
  • async (Boolean) (defaults to: false)

    (default: false)



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

def esql(esql, async: false)
  # build new query
  query = ElasticsearchRecord::Query.new(
    type: ElasticsearchRecord::Query::TYPE_ESQL,
    body: { query: esql },
    # IMPORTANT: Always provide all columns
    columns: source_column_names)

  connection.exec_query(query, "#{name} ES|QL", async: async)
end

#find_by_esql(esql, &block) ⇒ Object

ES|QL query API Returns search results for an ES|QL (Elasticsearch query language) query.

Parameters:

  • esql (String)
  • block (Proc)


85
86
87
88
89
90
91
92
93
94
# File 'lib/elasticsearch_record/querying.rb', line 85

def find_by_esql(esql, &block)
  # build new query
  query = ElasticsearchRecord::Query.new(
    type: ElasticsearchRecord::Query::TYPE_ESQL,
    body: { query: esql },
    # IMPORTANT: Always provide all columns
    columns: source_column_names)

  _load_from_sql(_query_by_sql(query), &block)
end

#find_by_id(id) ⇒ Object

finds a single record by provided id. This method is overwritten to support the primary key column (+_id+).

Parameters:

  • id (Object)


24
25
26
# File 'lib/elasticsearch_record/querying.rb', line 24

def find_by_id(id)
  has_attribute?('id') ? super(id) : public_send(:find_by__id, id)
end

#find_by_query(arguments, &block) ⇒ Object

finds records by query arguments



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elasticsearch_record/querying.rb', line 68

def find_by_query(arguments, &block)
  # build new query
  query = ElasticsearchRecord::Query.new(
    index:     table_name,
    type:      ElasticsearchRecord::Query::TYPE_SEARCH,
    arguments: arguments,
    # IMPORTANT: Always provide all columns to prevent unknown attributes that should be nil ...
    columns: source_column_names)

  _load_from_sql(_query_by_sql(query), &block)
end

#find_by_sql(sql, binds = [], preparable: nil, &block) ⇒ Object

finds records by sql, query-arguments or query-object.

PLEASE NOTE: This method is used by different other methods:

  • ActiveRecord::Relation#exec_queries
  • ActiveRecord::StatementCache#execute

We cannot rewrite all call-sources since this will mess up the whole logic end will end in other problems. So we check here what kind of query is provided and decide what to do.

PLEASE NOTE: since ths is also used by +ActiveRecord::StatementCache#execute+ we cannot remove the unused params +preparable+. see @ ActiveRecord::Querying#find_by_sql

Parameters:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elasticsearch_record/querying.rb', line 46

def find_by_sql(sql, binds = [], preparable: nil, &block)
  query = case sql
          when String # really find by SQL
            ElasticsearchRecord::Query.new(
              type: ElasticsearchRecord::Query::TYPE_SQL,
              body: { query: query_or_sql },
              # IMPORTANT: Always provide all columns
              columns: source_column_names)
          when Hash
            ElasticsearchRecord::Query.new(
              type:      ElasticsearchRecord::Query::TYPE_SEARCH,
              arguments: sql,
              # IMPORTANT: Always provide all columns
              columns: source_column_names)
          else
            sql
          end

  _load_from_sql(_query_by_sql(query, binds), &block)
end

#msearch(queries, async: false) ⇒ Object

executes a +msearch+ by provided RAW queries. Does NOT instantiate records.

Parameters:

  • queries (Array<String>)
  • async (Boolean) (defaults to: false)

    (default: false)



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/elasticsearch_record/querying.rb', line 116

def msearch(queries, async: false)
  # build new msearch query
  query = ElasticsearchRecord::Query.new(
    index: table_name,
    type:  ElasticsearchRecord::Query::TYPE_MSEARCH,
    body:  queries.map { |q| { search: q } },
    # IMPORTANT: Always provide all columns
    columns: source_column_names)

  connection.exec_query(query, "#{name} Msearch", async: async)
end

#search(*args, &block) ⇒ Object

executes a search by provided +RAW+ query - supports +Elasticsearch::DSL+ gem if loaded



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/elasticsearch_record/querying.rb', line 129

def search(*args, &block)
  begin
    # require the Elasticsearch::DSL gem, if loaded
    require 'elasticsearch/dsl'
    query = ::Elasticsearch::DSL::Search::Search.new(*args, &block).to_hash
  rescue LoadError
    query = args.extract_options!
  rescue
    query = args.extract_options!
  end

  find_by_query(query)
end