lib/inquisitio/searcher.rb in inquisitio-1.1.0 vs lib/inquisitio/searcher.rb in inquisitio-1.1.1
- old
+ new
@@ -7,10 +7,11 @@
def self.method_missing(name, *args)
Searcher.new.send(name, *args)
end
attr_reader :params
+
def initialize(params = nil)
@params = params || {
criteria: [],
filters: {},
per: 10,
@@ -26,35 +27,50 @@
def search
results
end
def ids
- @ids ||= map{|r|r['data']['id'].first}.flatten.map(&:to_i)
+ @ids ||= map { |r| r['data']['id'].first }.flatten.map(&:to_i)
end
def records
- @records ||= begin
- klasses = {}
- results.map do |result|
- klass = result['data']['type'].first
- klasses[klass] ||= []
- klasses[klass] << result['data']['id'].first
- end
+ return @records unless @records.nil?
- klasses.map {|klass, ids|
- klass = klass.gsub("_", "::")
- klass.constantize.where(id: ids)
- }.flatten
+ @records = []
+ klasses = {}
+ results.each do |result|
+ klass = result['data']['type'].first
+ id = result['data']['id'].first
+ klasses[klass] ||= []
+ klasses[klass] << id
end
+
+ objs = klasses.map { |klass_name, ids|
+ klass_name = klass_name.gsub("_", "::")
+ klass = klass_name.constantize
+ klass.where(id: ids)
+ }.flatten
+
+ results.each do |result|
+ klass_name = result['data']['type'].first
+ klass_name = klass_name.gsub("_", "::")
+ id = result['data']['id'].first
+ record = objs.select { |r|
+ r.class.name == klass_name && r.id == id.to_i
+ }.first
+ @records << record
+ end
+
+ return @records
end
def where(value)
clone do |s|
if value.is_a?(Array)
s.params[:criteria] += value
elsif value.is_a?(Hash)
- value.each do |k,v|
+ value.each do |k, v|
s.params[:filters][k] ||= []
if v.is_a?(Array)
s.params[:filters][k] = v
else
s.params[:filters][k] << v
@@ -108,13 +124,13 @@
Inquisitio.config.logger.info("Performing search: #{search_url}")
response = Excon.get(search_url)
raise InquisitioError.new("Search failed with status code: #{response.status} Message #{response.body}") unless response.status == 200
body = JSON.parse(response.body)
@results = Results.new(body["hits"]["hit"],
- params[:page],
- params[:per],
- body["hits"]["found"])
+ params[:page],
+ params[:per],
+ body["hits"]["found"])
rescue => e
@failed_attempts += 1
Inquisitio.config.logger.error("Exception Performing search: #{search_url} #{e}")
if @failed_attempts < Inquisitio.config.max_attempts
@@ -125,10 +141,10 @@
end
end
def search_url
@search_url ||= begin
- return_fields = params[:returns].empty?? [:type, :id] : params[:returns]
+ return_fields = params[:returns].empty? ? [:type, :id] : params[:returns]
SearchUrlBuilder.build(
query: params[:criteria],
filters: params[:filters],
arguments: params[:with].merge({