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



108
109
110
111
112
# File 'lib/custom_attributes/fluent_search_query.rb', line 108

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

#match_anyObject



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

def match_any
  @search_query.match_any = true

  self
end

#page(page) ⇒ Object



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

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

  self
end

#per_page(per_page) ⇒ Object



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

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!



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

def query_hash
  process_field_list

  @search_query.build
end

#rawObject

Use at own risk, no Connection Exception handling here



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

def raw
  @raw_results
end

#recordsObject

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/custom_attributes/fluent_search_query.rb', line 95

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!



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/custom_attributes/fluent_search_query.rb', line 81

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



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

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

  self
end

#sort_by(field_list) ⇒ Object



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

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

  self
end