Class: Elastic::ESQL

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/esql.rb

Overview

Examples:

Elastic::ESQL.from('sample_data').sort_descending('@timestamp').limit(3).to_s
# => FROM 'sample_data' | SORT @timestamp desc | LIMIT 3

Constant Summary collapse

SOURCE_COMMANDS =
[:from, :promql, :row, :show, :ts].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeESQL

Returns a new instance of ESQL.



64
65
66
67
68
# File 'lib/elastic/esql.rb', line 64

def initialize
  @query = {}
  @custom = []
  @metadata = []
end

Class Method Details

.branchObject

rubocop:enable Naming/MethodName, Naming/BinaryOperatorParameterName



146
147
148
# File 'lib/elastic/esql.rb', line 146

def self.branch
  Branch.new
end

.chickenObject



137
138
139
# File 'lib/elastic/esql.rb', line 137

def πŸ”(message)
  "ROW CHICKEN(\"#{message}\")"
end

.πŸ”(message) ⇒ Object



134
135
136
# File 'lib/elastic/esql.rb', line 134

def πŸ”(message)
  "ROW CHICKEN(\"#{message}\")"
end

Instance Method Details

#enrich(policy) ⇒ Elastic::Enrich

Creates a new Enrich object to chain with on and with. If other method is chained to the Enrich object, it calls it upon the ESQL object that instantiated it, and returns it.

Returns:

  • (Elastic::Enrich)


104
105
106
107
108
109
# File 'lib/elastic/esql.rb', line 104

def enrich(policy)
  @enriches ||= []
  enrich = Enrich.new(policy, self)
  @enriches << enrich
  enrich
end

#from(index_pattern) ⇒ Object

Instance method to allow to update from with esql.from(β€˜different_source’).

Parameters:

  • index_pattern (String)

    A list of indices, data streams or aliases. Supports wildcards and date math.



121
122
123
124
# File 'lib/elastic/esql.rb', line 121

def from(index_pattern)
  @query = { from: index_pattern }
  self
end

#queryString

Function to build the ES|QL formatted query and return it as a String.

Returns:

  • (String)

    The ES|QL query in ES|QL format.

Raises:

  • (ArgumentError)

    if the query has no source command



90
91
92
93
94
95
96
97
98
99
# File 'lib/elastic/esql.rb', line 90

def query
  raise ArgumentError, 'No source command found' unless source_command_present?

  string_query = @set ? "SET #{@set};\n" : ''
  @query[:enrich] = @enriches.map(&:to_query).join('| ') if @enriches
  @query[:rerank] = @rerank.to_query if @rerank
  string_query.concat(build_string_query)
  string_query.concat(" #{@custom.join(' ')}") unless @custom.empty?
  string_query
end

#rerank(column: nil, query: '') ⇒ Elastic::Rerank

Creates a new Rerank object to chain with on and with. If other method is chained to the Rerank object, it calls it upon the ESQL object that instantiated it, and returns it.

Returns:

  • (Elastic::Rerank)


114
115
116
117
# File 'lib/elastic/esql.rb', line 114

def rerank(column: nil, query: '')
  @rerank = Rerank.new(self, column: column, query: query)
  @rerank
end

#to_sObject

Defining to_s so the ES|QL formatted query is returned. This way the query will be serialized when passing an Elastic::ESQL object to the Elasticsearch client and other libraries.



128
129
130
# File 'lib/elastic/esql.rb', line 128

def to_s
  query
end

#user_agent(params) ⇒ Elastic::UserAgent

Creates a new UserAgent object to chain with with. If other method is chained to the UserAgent object, it calls it upon the ESQL object that instantiated it, and returns it.

Returns:

  • (Elastic::UserAgent)


153
154
155
# File 'lib/elastic/esql.rb', line 153

def user_agent(params)
  UserAgent.new(params, self)
end

#πŸ”(message) ⇒ Object Also known as: chicken



140
141
142
# File 'lib/elastic/esql.rb', line 140

def πŸ”(message)
  self.class.πŸ”(message)
end