Class: Dilute::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dilute/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(for_class, options = {}, chained = true) ⇒ Query

Returns a new instance of Query.



5
6
7
# File 'lib/dilute/query.rb', line 5

def initialize(for_class, options = {}, chained = true)
  @for_class, @options, @chained = for_class, options, chained
end

Instance Attribute Details

#chainedObject (readonly)

Returns the value of attribute chained.



2
3
4
# File 'lib/dilute/query.rb', line 2

def chained
  @chained
end

#for_classObject (readonly)

Returns the value of attribute for_class.



2
3
4
# File 'lib/dilute/query.rb', line 2

def for_class
  @for_class
end

#from_elastic_searchObject (readonly)

Returns the value of attribute from_elastic_search.



2
3
4
# File 'lib/dilute/query.rb', line 2

def from_elastic_search
  @from_elastic_search
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/dilute/query.rb', line 2

def options
  @options
end

#resultsObject (readonly)

Returns the value of attribute results.



2
3
4
# File 'lib/dilute/query.rb', line 2

def results
  @results
end

Instance Method Details

#each(*args, &block) ⇒ Object



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

def each(*args, &block)
  return enum_for(__callee__) unless block_given?
  to_a.each(*args, &block)
end

#execute!Object



19
20
21
22
23
24
25
# File 'lib/dilute/query.rb', line 19

def execute!
  @results ||= begin
    search_results = for_class.type.search(size: 25, query: to_query)
    @from_elastic_search = search_results
    search_results.raw["hits"]["hits"].collect {|r| for_class.new(r) }
  end
end

#match(q) ⇒ Object



9
10
11
# File 'lib/dilute/query.rb', line 9

def match(q)
  merge_or_chain(:match, q)
end

#to_aObject



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

def to_a
  execute!
  results
end

#to_queryObject



13
14
15
16
17
# File 'lib/dilute/query.rb', line 13

def to_query
  query = options.reject {|k,v| v.nil? || v.empty? }
  query[:match_all] ||= {} unless query[:match]
  query
end