class Sunspot::Rails::StubSessionProxy::Search
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
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
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
Attributes
Public Class Methods
# File lib/stub_solr/search.rb, line 13 def initialize(results = [], types = [], &block) @types = types @grouped_results= {} @group_key = '' @range_search_field = nil @operation_context = [{ operation: 'all', result: results, order_key: nil, order_direction: nil, page: 1, per_page: 30, search_term: '', textsearch_priorities: {} }] @current_index = 0 run_proc_block(&block) end
Public Instance Methods
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
# File lib/stub_solr/extra_dsl.rb, line 19 def [](key) return if @grouped_results.empty? || @grouped_results[key].nil? @group_key = key self end
# File lib/stub_solr/search.rb, line 57 def all_of(&bl) previous_result = result_for_current_context push(previous_result, :all) Sunspot::Util::ContextBoundDelegate.instance_eval_with_context(self, &bl) after_results = result_for_current_context parent_temp_result = @operation_context[@current_index - 1][:temp_result] pop unless @current_index == 0 # parent is now current context if parent_temp_result @operation_context[@current_index][:temp_result] = parent_temp_result.concat(after_results) else @operation_context[@current_index][:result] = (previous_result & after_results).uniq end self end
# File lib/stub_solr/search.rb, line 40 def any_of(&bl) previous_result = result_for_current_context push(previous_result, :any) Sunspot::Util::ContextBoundDelegate.instance_eval_with_context(self, &bl) after_results = temp_result_for_current_context pop unless @current_index == 0 # parent is now current context if parent_temp_result @operation_context[@current_index][:temp_result] = parent_temp_result.concat(after_results) else @operation_context[@current_index][:result] = after_results end self end
# File lib/stub_solr/extra_dsl.rb, line 136 def filter(array) array.select do |i| i.read_attribute(@range_search_field) && !i.read_attribute(@range_search_field).nil? && yield(i) end.uniq end
# File lib/stub_solr/search.rb, line 26 def final_results @operation_context.last[:result] end
with default `StubSessionProxy`, the results is blank whatever argument or options you put in the search block
# File lib/stub_solr/extra_dsl.rb, line 59 def fulltext(term, _opt = {}, &bl) matches = [] @operation_context.first[:search_term] = term Sunspot::Util::ContextBoundDelegate .instance_eval_with_context(self, &bl) unless 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
# File lib/stub_solr/extra_dsl.rb, line 94 def greater_than(time) calculated = filter(result_for_current_context) do |x| x.read_attribute(@range_search_field) > time end operation_context_result(calculated) self end
# File lib/stub_solr/extra_dsl.rb, line 102 def greater_than_or_equal_to(time) calculated = filter(result_for_current_context) do |x| x.read_attribute(@range_search_field) >= time end operation_context_result(calculated) self end
possible situation
initial_grouped_hits = initial.hits.group_by(&:class_name)
# File lib/stub_solr/extra_dsl.rb, line 37 def group_by(_ = nil) return self if final_results.empty? @types.each do |type| @grouped_results[type.name] = sorted_temp_result.group_by do |i| i.class.name == type.name end[true] end self end
# File lib/stub_solr/extra_dsl.rb, line 9 def hits self end
# File lib/stub_solr/extra_dsl.rb, line 85 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
# File lib/stub_solr/extra_dsl.rb, line 110 def less_than(time) calculated = filter(result_for_current_context) do |x| x.read_attribute(@range_search_field) < time end operation_context_result(calculated) self end
# File lib/stub_solr/extra_dsl.rb, line 118 def less_than_or_equal_to(time) calculated = filter(result_for_current_context) do |x| x.read_attribute(@range_search_field) <= time end operation_context_result(calculated) self end
possible situation
initial_grouped_hits = initial.hits.group_by(&:class_name) initial_grouped_hits.map(&:primary_key)
# File lib/stub_solr/extra_dsl.rb, line 29 def map(&_pr) return unless @grouped_results[@group_key] @grouped_results[@group_key].map(&:id) end
# File lib/stub_solr/extra_dsl.rb, line 79 def order_by(attribute, direction) @operation_context.first[:order_key] = attribute @operation_context.first[:order_direction] = direction self end
# File lib/stub_solr/extra_dsl.rb, line 126 def paginate(args = {}) @operation_context.first[:page] = args[:page] @operation_context.first[:per_page] = args[:page] self end
# File lib/stub_solr/extra_dsl.rb, line 47 def phrase_fields(arg) @operation_context.first[:textsearch_priorities] = arg return self if @operation_context.first[:search_term].empty? fulltext(@operation_context.first[:search_term]) end
# File lib/stub_solr/extra_dsl.rb, line 53 def query_phrase_slop(_) fulltext(@operation_context.first[:search_term]) end
# File lib/stub_solr/search.rb, line 30 def results return PaginatedCollection.new if sorted_temp_result.empty? if final_results.is_a? Kaminari::PaginatableArray return sorted_temp_result end Kaminari.paginate_array(sorted_temp_result). page(@operation_context.first[:page]). per(@operation_context.first[:per_page]) end
# File lib/stub_solr/extra_dsl.rb, line 132 def total @operation_context.last[:result].size end
# File lib/stub_solr/search.rb, line 76 def with(att, val = 'no_value') # return_early_for_with_blocks(att, val, :with) return self if range_no_value?(att, val) && skip_it(att, val) return self if range_value_nil?(val, att) && deal_with_nil(att, :with) return self if val == 'no_value' || att == :location return self if (att == :search_class) && filter_class matches = if val.class.name == 'Array' select_result_for_array_val(att) elsif @types.map{ |x| x.has_attribute?(att)}.include?(true) select_result_attr_is_val(att, val) else result_for_current_context.select { |x| x.send(att) == val } end operation_context_result(matches) self end
# File lib/stub_solr/search.rb, line 93 def without(att, val = 'no_value') # return_early_for_with_blocks(att, val, :without) return self if range_no_value?(att, val) && skip_it(att, val) return self if range_value_nil?(val, att) && deal_with_nil(att, :without) return self if val == 'no_value' || att == :location matches = if val.class.name == 'Array' select_result_attr_cant_send_val(att, val) else select_result_attr_is_not_value(att, val) end operation_context_result(matches) self end
Private Instance Methods
# File lib/stub_solr/search_helper.rb, line 107 def attribute_is_about_range?(attribute) matched = nil Sunspot.session.range_fields.each do |x| if attribute.to_s.include? x matched = true @range_search_field = attribute next end end matched end
# File lib/stub_solr/search_helper.rb, line 28 def current_operation @operation_context[@current_index][:operation] end
# File lib/stub_solr/search_helper.rb, line 92 def deal_with_nil(attribute, operation) matches = if operation == :with selelct_result_for_nil(attribute) else select_result_for_not_nil(attribute) end operation_context_result(matches) end
# File lib/stub_solr/search_helper.rb, line 72 def filter_class matches = result_for_current_context.select { |x| x.class.name == value } operation_context_result(matches) end
# File lib/stub_solr/search_helper.rb, line 14 def instance_eval_with_context(&block) Sunspot::Util::ContextBoundDelegate. instance_eval_with_context(self, &block) end
# File lib/stub_solr/search_helper.rb, line 44 def operation_context_result(after_results) previous_result = result_for_current_context unless current_operation == 'any' return @operation_context[@current_index][:result] = (previous_result & after_results).uniq end @operation_context[@current_index][:result] = previous_result.concat(after_results).uniq if temp_result_for_current_context @operation_context[@current_index][:temp_result] = temp_result_for_current_context.concat(after_results) end end
# File lib/stub_solr/search_helper.rb, line 40 def parent_temp_result @operation_context[@current_index - 1][:temp_result] end
# File lib/stub_solr/search_helper.rb, line 58 def pop @current_index -= 1 @operation_context.pop end
# File lib/stub_solr/search_helper.rb, line 63 def push(data, type) if type == :any @operation_context.push(operation: 'any', result: data, temp_result: []) else @operation_context.push(operation: 'all', result: data) end @current_index += 1 end
# File lib/stub_solr/search_helper.rb, line 81 def range_no_value?(attribute, value) (value == 'no_value') && attribute_is_about_range?(attribute) end
# File lib/stub_solr/search_helper.rb, line 77 def range_value_nil?(value, attribute) value.nil? && attribute_is_about_range?(attribute) end
# File lib/stub_solr/search_helper.rb, line 32 def result_for_current_context @operation_context[@current_index][:result] end
iterate the given block and change @operation_context block may cotain with, without, fulltext, paginate, in_raius, order_by
# File lib/stub_solr/search_helper.rb, line 21 def run_proc_block(&block) result = @operation_context.last[:result] return PaginatedCollection.new if result.empty? instance_eval_with_context(&block) self end
# File lib/stub_solr/search_helper.rb, line 154 def select_result_attr_cant_send_val(attribute, value) result_for_current_context.select do |x| x.has_attribute?(attribute) && !value.include?(x.send(attribute)) end end
# File lib/stub_solr/search_helper.rb, line 160 def select_result_attr_is_not_value(attribute, value) result_for_current_context.select do |x| x.has_attribute?(attribute) && !x.read_attribute(attribute).nil? && (x.read_attribute(attribute) != value) end end
# File lib/stub_solr/search_helper.rb, line 148 def select_result_attr_is_val(attribute, value) result_for_current_context.select do |x| x.read_attribute(attribute) == value end end
# File lib/stub_solr/search_helper.rb, line 142 def select_result_for_array_val(attribute) result_for_current_context.select do |x| value.include?(x.send(attribute)) end end
# File lib/stub_solr/search_helper.rb, line 135 def select_result_for_not_nil(attribute) result_for_current_context.select do |x| x.has_attribute?(attribute) && !x.read_attribute(attribute).nil? end end
# File lib/stub_solr/search_helper.rb, line 128 def selelct_result_for_nil(attribute) result_for_current_context.select do |x| x.has_attribute?(attribute) && x.read_attribute(attribute).nil? end end
# File lib/stub_solr/search_helper.rb, line 85 def skip_it(attribute, value) matches = result_for_current_context.select do |x| x.has_attribute?(attribute) end operation_context_result(matches) end
# File lib/stub_solr/search_helper.rb, line 119 def sorted_temp_result key = @operation_context.first[:order_key] direction = @operation_context.first[:order_direction] return final_results unless direction asc = final_results.sort_by { |x| x[key] } return asc if direction.to_s.downcase.include?('asc') asc.reverse end
# File lib/stub_solr/search_helper.rb, line 101 def string_text_fields(type) pr = @operation_context.first[:textsearch_priorities] return pr.keys unless pr.keys.empty? type.columns_hash.select { |k,v| [:string, :text].include? v.type }.keys end
# File lib/stub_solr/search_helper.rb, line 36 def temp_result_for_current_context @operation_context[@current_index][:temp_result] end