Class: Elastic::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Dsl::BoolQueryBuilder, Dsl::MetricBuilder, Railties::QueryExtensions, Enumerable
Defined in:
lib/elastic/railtie.rb,
lib/elastic/query.rb

Overview

Add activerecord related query helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Railties::QueryExtensions

#includes

Methods included from Dsl::MetricBuilder

#average, #closing, #maximum, #minimum, #opening, #stats, #sum

Methods included from Dsl::BoolQueryBuilder

#boost, #coord_similarity, #must, #must_not, #should, #with

Constructor Details

#initialize(_index, _query_config = nil) ⇒ Query

Returns a new instance of Query.



12
13
14
15
# File 'lib/elastic/query.rb', line 12

def initialize(_index, _query_config = nil)
  @index = _index
  @config = _query_config || build_base_config
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



10
11
12
# File 'lib/elastic/query.rb', line 10

def index
  @index
end

Instance Method Details

#aggregate(_node) ⇒ Object



69
70
71
# File 'lib/elastic/query.rb', line 69

def aggregate(_node)
  execute assembler.assemble_metric _node
end

#as_es_queryObject



59
60
61
# File 'lib/elastic/query.rb', line 59

def as_es_query
  assembler.assemble.render
end

#as_query_nodeObject



55
56
57
# File 'lib/elastic/query.rb', line 55

def as_query_node
  @config.query.clone
end

#compose(&_block) ⇒ Object



63
64
65
66
67
# File 'lib/elastic/query.rb', line 63

def compose(&_block)
  agg_nodes = []
  Dsl::ResultComposer.new(agg_nodes).tap(&_block)
  execute assembler.assemble_metrics agg_nodes
end

#idsObject



38
39
40
# File 'lib/elastic/query.rb', line 38

def ids
  execute assembler.assemble_ids
end

#limit(_size) ⇒ Object Also known as: size



17
18
19
# File 'lib/elastic/query.rb', line 17

def limit(_size)
  with_clone { |config| config.limit = _size }
end

#offset(_offset) ⇒ Object



22
23
24
# File 'lib/elastic/query.rb', line 22

def offset(_offset)
  with_clone { |config| config.offset = _offset }
end

#pick(_field) ⇒ Object



42
43
44
# File 'lib/elastic/query.rb', line 42

def pick(_field)
  execute assembler.assemble_pick(_field)
end

#result(_reset = false) ⇒ Object



50
51
52
53
# File 'lib/elastic/query.rb', line 50

def result(_reset = false)
  @result = nil if _reset
  @result ||= execute(assembler.assemble)
end

#segment(*_params) ⇒ Object



32
33
34
35
36
# File 'lib/elastic/query.rb', line 32

def segment(*_params)
  with_clone do |config|
    config.groups << Commands::BuildAggFromParams.for(index: index, params: _params)
  end
end

#sort(*_params) ⇒ Object



26
27
28
29
30
# File 'lib/elastic/query.rb', line 26

def sort(*_params)
  with_clone do |config|
    config.sort = Commands::BuildSortFromParams.for(index: index, params: _params)
  end
end

#totalObject



46
47
48
# File 'lib/elastic/query.rb', line 46

def total
  execute assembler.assemble_total
end