Class: Sunspot::Rails::StubSessionProxy::Search
- Inherits:
-
Object
- Object
- Sunspot::Rails::StubSessionProxy::Search
- Defined in:
- lib/stub_solr/sunspot_rails_stub_session_proxy.rb
Overview
with and fulltext can be called multiple times based on the block. that’s why i am mutating @results
for all and any block, we need to consult sunspot/lib/sunspot/dsl/standard_query.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#grouped_results ⇒ Object
readonly
Returns the value of attribute grouped_results.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Added to pass .group_by Idea is save grouped hash result seperately and then access it with [] method.
- #all_of(&bl) ⇒ Object
- #any_of(&bl) ⇒ Object
- #final_results ⇒ Object
-
#fulltext(term, opt = {}, &bl) ⇒ Object
with default ‘StubSessionProxy`, the results is blank whatever argument or options you put in the search block.
- #greater_than(time) ⇒ Object
- #greater_than_or_equal_to(time) ⇒ Object
-
#group_by(class_name = nil) ⇒ Object
possible situation.
- #hits ⇒ Object
- #in_radius(*args) ⇒ Object
-
#initialize(results = [], types = [], &block) ⇒ Search
constructor
A new instance of Search.
- #less_than(time) ⇒ Object
- #less_than_or_equal_to(time) ⇒ Object
-
#map(&pr) ⇒ Object
possible situation.
- #order_by(attribute, direction) ⇒ Object
- #paginate(args = {}) ⇒ Object
- #phrase_fields(arg) ⇒ Object
- #query_phrase_slop(*args) ⇒ Object
- #results ⇒ Object
- #total ⇒ Object
- #with(attribute, value = "no_value") ⇒ Object
- #without(attribute, value = "no_value") ⇒ Object
Constructor Details
#initialize(results = [], types = [], &block) ⇒ Search
Returns a new instance of Search.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 35 def initialize(results = [], types = [], &block) @facets = {} @search_data = results @block = block @types = types @page = 1 @per_page = 30 @grouped_results = {} @group_key = "" @range_search_field = nil @operation_context = [{operation: "all", result: @search_data}] @current_context_index = 0 @order_key = nil @order_direction = nil @textsearch_priorities = {} @search_term = "" run_proc_block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
34 35 36 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 34 def block @block end |
#grouped_results ⇒ Object (readonly)
Returns the value of attribute grouped_results.
34 35 36 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 34 def grouped_results @grouped_results end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
34 35 36 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 34 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
34 35 36 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 34 def per_page @per_page end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
34 35 36 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 34 def types @types end |
Instance Method Details
#[](key) ⇒ Object
Added to pass .group_by Idea is save grouped hash result seperately and then access it with [] method.
initial_grouped_hits = initial.hits.group_by(&:class_name) initial_grouped_hits
168 169 170 171 172 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 168 def [](key) return if @grouped_results.empty? || @grouped_results[key].nil? @group_key = key self end |
#all_of(&bl) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 88 def all_of(&bl) previous_result = result_for_current_context @operation_context.push({operation: "all", result: previous_result}) @current_context_index += 1 Sunspot::Util::ContextBoundDelegate.instance_eval_with_context(self, &bl) after_results = result_for_current_context parent_temp_result = @operation_context[@current_context_index - 1][:temp_result] unless @current_context_index == 0 @current_context_index -= 1 @operation_context.pop end # parent is now current context if parent_temp_result @operation_context[@current_context_index][:temp_result] = parent_temp_result.concat(after_results) else @operation_context[@current_context_index][:result] = (previous_result & after_results).uniq end self end |
#any_of(&bl) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 64 def any_of(&bl) previous_result = result_for_current_context @operation_context.push({operation: "any", result: previous_result, temp_result: []}) @current_context_index += 1 Sunspot::Util::ContextBoundDelegate.instance_eval_with_context(self, &bl) after_results = @operation_context[@current_context_index][:temp_result] parent_temp_result = @operation_context[@current_context_index - 1][:temp_result] unless @current_context_index == 0 @current_context_index -= 1 @operation_context.pop end # parent is now current context if parent_temp_result @operation_context[@current_context_index][:temp_result] = parent_temp_result.concat(after_results) else @operation_context[@current_context_index][:result] = after_results end self end |
#final_results ⇒ Object
54 55 56 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 54 def final_results @operation_context.last[:result] end |
#fulltext(term, opt = {}, &bl) ⇒ Object
with default ‘StubSessionProxy`, the results is blank whatever argument or options you put in the search block
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 206 def fulltext(term, opt={}, &bl) matches = [] @search_term = term Sunspot::Util::ContextBoundDelegate.instance_eval_with_context(self, &bl) if !bl.nil? # if there is phrase_fields option then we only search for the field # to mimic the priority search. #string_text_fields @types.each do |type| string_text_fields(type).each do |field| if type.has_attribute?(field.to_sym) matches << type.where(type.arel_table[field.to_sym].matches("%#{term}%")).to_a end end end calculated = (result_for_current_context & matches.flatten.uniq) operation_context_result(calculated) self end |
#greater_than(time) ⇒ Object
239 240 241 242 243 244 245 246 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 239 def greater_than(time) calculated = result_for_current_context.select do |x| has_attribute_and_not_nil(x) && x.read_attribute(@range_search_field) > time end.uniq operation_context_result(calculated) self end |
#greater_than_or_equal_to(time) ⇒ Object
248 249 250 251 252 253 254 255 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 248 def greater_than_or_equal_to(time) calculated = result_for_current_context.select do |x| has_attribute_and_not_nil(x) && x.read_attribute(@range_search_field) >= time end.uniq operation_context_result(calculated) self end |
#group_by(class_name = nil) ⇒ Object
possible situation
initial_grouped_hits = initial.hits.group_by(&:class_name)
186 187 188 189 190 191 192 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 186 def group_by(class_name = nil) return self if final_results.empty? @types.each do |type| @grouped_results[type.name] = sorted_temp_result.group_by{|i| i.class.name == type.name }[true] end self end |
#hits ⇒ Object
158 159 160 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 158 def hits self end |
#in_radius(*args) ⇒ Object
230 231 232 233 234 235 236 237 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 230 def in_radius(*args) calculated = result_for_current_context.select do |r| distance = r&.location&.distance_from([args[0], args[1]]) distance && distance < args[2].to_i end operation_context_result(calculated) self end |
#less_than(time) ⇒ Object
257 258 259 260 261 262 263 264 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 257 def less_than(time) calculated = result_for_current_context.select do |x| has_attribute_and_not_nil(x) && x.read_attribute(@range_search_field) < time end.uniq operation_context_result(calculated) self end |
#less_than_or_equal_to(time) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 266 def less_than_or_equal_to(time) calculated = result_for_current_context.select do |x| has_attribute_and_not_nil(x) && x.read_attribute(@range_search_field) <= time end.uniq operation_context_result(calculated) self end |
#map(&pr) ⇒ Object
possible situation
initial_grouped_hits = initial.hits.group_by(&:class_name) initial_grouped_hits.map(&:primary_key)
178 179 180 181 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 178 def map(&pr) return unless @grouped_results[@group_key] @grouped_results[@group_key].map{|x| x.id} end |
#order_by(attribute, direction) ⇒ Object
224 225 226 227 228 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 224 def order_by(attribute, direction) @order_key = attribute @order_direction = direction self end |
#paginate(args = {}) ⇒ Object
275 276 277 278 279 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 275 def paginate(args={}) @page = args[:page] @per_page = args[:per_page] self end |
#phrase_fields(arg) ⇒ Object
194 195 196 197 198 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 194 def phrase_fields(arg) @textsearch_priorities = arg return self if @search_term.empty? fulltext(@search_term) end |
#query_phrase_slop(*args) ⇒ Object
200 201 202 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 200 def query_phrase_slop(*args) fulltext(@search_term) end |
#results ⇒ Object
58 59 60 61 62 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 58 def results return PaginatedCollection.new if sorted_temp_result.empty? return sorted_temp_result if final_results.is_a? Kaminari::PaginatableArray Kaminari.paginate_array(sorted_temp_result).page(@page).per(@per_page) end |
#total ⇒ Object
281 282 283 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 281 def total @operation_context.last[:result].size end |
#with(attribute, value = "no_value") ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 112 def with(attribute, value = "no_value") skip_no_value(attribute, value) if attribute == :search_class matches = result_for_current_context.select {|x| x.class.name == value } operation_context_result(matches) return self end if value == nil && attribute_is_about_range?(attribute) matches = result_for_current_context.select {|x| x.has_attribute?(attribute) && (x.read_attribute(attribute) == value)} operation_context_result(matches) return self end return self if value == "no_value" || attribute == :location matches = if value.class.name == "Array" result_for_current_context.select {|x| value.include?(x.send(attribute)) } elsif @types.map{|x| x.has_attribute?(attribute)}.include?(true) result_for_current_context.select {|x| x.read_attribute(attribute) == value } else result_for_current_context.select {|x| x.send(attribute) == value } end operation_context_result(matches) self end |
#without(attribute, value = "no_value") ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/stub_solr/sunspot_rails_stub_session_proxy.rb', line 140 def without(attribute, value = "no_value") skip_no_value(attribute, value) if value == nil && attribute_is_about_range?(attribute) matches = result_for_current_context.select {|x| x.has_attribute?(attribute) && !(x.read_attribute(attribute) == value)} operation_context_result(matches) return self end return self if value == "no_value" || attribute == :location matches = if value.class.name == "Array" result_for_current_context.select {|x| x.has_attribute?(attribute) && !value.include?(x.send(attribute)) } else result_for_current_context.select {|x| x.has_attribute?(attribute) && !x.read_attribute(attribute).nil? && (x.read_attribute(attribute) != value) } end operation_context_result(matches) self end |