Class: CustomAttributes::FluentSearchQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_attributes/fluent_search_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(customizable) ⇒ FluentSearchQuery

Returns a new instance of FluentSearchQuery.



5
6
7
8
9
10
11
12
13
# File 'lib/custom_attributes/fluent_search_query.rb', line 5

def initialize(customizable)
  @search_query = CustomAttributes::SearchQuery.new
  @search_query.customizable = customizable unless customizable.nil?

  @raw_results = nil
  @field_list = {}
  @field_queries = {}
  @field_config = {}
end

Instance Method Details

#countObject

Performs the search and returns the count of results



114
115
116
117
118
# File 'lib/custom_attributes/fluent_search_query.rb', line 114

def count
  search.results.count
rescue Faraday::ConnectionFailed => e
  @search_query.customizable.class.handle_search_connection_error(e)
end

#field_config(field_config) ⇒ Object



36
37
38
39
40
# File 'lib/custom_attributes/fluent_search_query.rb', line 36

def field_config(field_config)
  @field_config = field_config

  self
end

#field_list(field_list) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/custom_attributes/fluent_search_query.rb', line 21

def field_list(field_list)
  @field_list = Hash[field_list.map do |type, fields|
    fields = fields.split(',') if fields.is_a? String
    [type, fields]
  end]

  self
end

#field_queries(field_queries) ⇒ Object



30
31
32
33
34
# File 'lib/custom_attributes/fluent_search_query.rb', line 30

def field_queries(field_queries)
  @field_queries = field_queries

  self
end

#filter_by(field_list) ⇒ Object



42
43
44
45
46
# File 'lib/custom_attributes/fluent_search_query.rb', line 42

def filter_by(field_list)
  @search_query.filter_by.merge! field_list

  self
end

#filter_or(field_list) ⇒ Object



48
49
50
51
52
# File 'lib/custom_attributes/fluent_search_query.rb', line 48

def filter_or(field_list)
  @search_query.filter_or.merge! field_list

  self
end

#match_anyObject



60
61
62
63
64
# File 'lib/custom_attributes/fluent_search_query.rb', line 60

def match_any
  @search_query.match_any = true

  self
end

#page(page) ⇒ Object



66
67
68
69
70
# File 'lib/custom_attributes/fluent_search_query.rb', line 66

def page(page)
  @search_query.page = page.to_i

  self
end

#per_page(per_page) ⇒ Object



72
73
74
75
76
# File 'lib/custom_attributes/fluent_search_query.rb', line 72

def per_page(per_page)
  @search_query.per_page = per_page.to_i

  self
end

#query(query) ⇒ Object



15
16
17
18
19
# File 'lib/custom_attributes/fluent_search_query.rb', line 15

def query(query)
  @search_query.query = query

  self
end

#query_hashObject

Return the hash that is conform to elasticsearch’s DSL Does not perform a search query!



127
128
129
130
131
# File 'lib/custom_attributes/fluent_search_query.rb', line 127

def query_hash
  process_field_list

  @search_query.build
end

#rawObject

Use at own risk, no Connection Exception handling here



121
122
123
# File 'lib/custom_attributes/fluent_search_query.rb', line 121

def raw
  @raw_results
end

#recordsObject

Fetches the ActiveRecord database records Does not perform search, so call search() first!



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/custom_attributes/fluent_search_query.rb', line 101

def records
  return nil if @raw_results.nil?

  begin
    @raw_results.count

    @raw_results.records
  rescue Faraday::ConnectionFailed => e
    @search_query.customizable.class.handle_search_connection_error(e)
  end
end

#resultsObject

Returns the results after search has been performed Call search() first!



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/custom_attributes/fluent_search_query.rb', line 87

def results
  return nil if @raw_results.nil?

  begin
    @raw_results.count

    @raw_results.results
  rescue Faraday::ConnectionFailed => e
    @search_query.customizable.class.handle_search_connection_error(e)
  end
end

#searchObject

Performs the search



79
80
81
82
83
# File 'lib/custom_attributes/fluent_search_query.rb', line 79

def search
  @raw_results = @search_query.customizable.class.__elasticsearch__.search(query_hash)

  self
end

#sort_by(field_list) ⇒ Object



54
55
56
57
58
# File 'lib/custom_attributes/fluent_search_query.rb', line 54

def sort_by(field_list)
  @search_query.sort_by.merge! field_list

  self
end