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



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

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_by_delimiter(string) ⇒ Object



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

def filter_by_delimiter(string)
  @search_query.filter_by_delimiter = string

  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

#filter_or_delimiter(string) ⇒ Object



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

def filter_or_delimiter(string)
  @search_query.filter_or_delimiter = string

  self
end

#match_anyObject



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

def match_any
  @search_query.match_any = true

  self
end

#page(page) ⇒ Object



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

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

  self
end

#per_page(per_page) ⇒ Object



84
85
86
87
88
# File 'lib/custom_attributes/fluent_search_query.rb', line 84

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!



139
140
141
142
143
# File 'lib/custom_attributes/fluent_search_query.rb', line 139

def query_hash
  process_field_list

  @search_query.build
end

#rawObject

Use at own risk, no Connection Exception handling here



133
134
135
# File 'lib/custom_attributes/fluent_search_query.rb', line 133

def raw
  @raw_results
end

#recordsObject

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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/custom_attributes/fluent_search_query.rb', line 113

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!



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

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



91
92
93
94
95
# File 'lib/custom_attributes/fluent_search_query.rb', line 91

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

  self
end

#sort_by(field_list) ⇒ Object



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

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

  self
end