class Stretcher::SearchResults
Conveniently represents elastic search results in a more compact fashion
Available properties:
-
raw : The raw response from elastic search
-
total : The total number of matched docs
-
facets : the facets hash
-
results : The hit results with _id merged in to _source
Public Class Methods
# File lib/stretcher/search_results.rb, line 11 def initialize(raw) @raw_plain = raw end
Public Instance Methods
Returns a ‘prettier’ version of elasticsearch results Also aliased as docs
This will:
-
Return either ‘_source’ or ‘fields’ as the base of the result
-
Merge any keys beginning with a ‘_’ into it as well (such as ‘_score’)
-
Copy the ‘highlight’ field into ‘_highlight’
# File lib/stretcher/search_results.rb, line 46 def documents # This function and its helpers are side-effecty for speed @documents ||= raw[:hits][:hits].map do |hit| doc = extract_source(hit) copy_underscores(hit, doc) copy_highlight(hit, doc) doc end end
Returns the facet data from elasticsearch Equivalent to raw
# File lib/stretcher/search_results.rb, line 34 def facets @facets ||= raw[:facets] end
Returns a Hashie::Mash version of the raw response
# File lib/stretcher/search_results.rb, line 23 def raw @raw ||= Hashie::Mash.new(@raw_plain) end
Returns a plain (string keyed) hash of the raw response Normally stretcher deals in Hashie::Mash-ified versions of data If you have truly gigantic result sets this may matter.
# File lib/stretcher/search_results.rb, line 18 def raw_plain @raw_plain end
DEPRECATED! Call documents
instead!
# File lib/stretcher/search_results.rb, line 59 def results documents end
Returns the total number of results
# File lib/stretcher/search_results.rb, line 28 def total @total ||= raw_plain['hits']['total'] end
Private Instance Methods
# File lib/stretcher/search_results.rb, line 87 def copy_highlight(hit, doc) if highlight = hit["highlight"] doc[:_highlight] = highlight end doc end
# File lib/stretcher/search_results.rb, line 78 def copy_underscores(hit, doc) # Copy underscore keys into the document hit.each do |k,v| doc[k] = v if k && k[0] == "_" end doc end
# File lib/stretcher/search_results.rb, line 65 def extract_source(hit) # Memoize the key, since it will be uniform across results @doc_key ||= if hit.key?(:_source) :_source elsif hit.key?(:fields) :fields else nil end Hashie::Mash.new(@doc_key ? hit[@doc_key] : Hashie::Mash.new) end