class Stretcher::SearchResults

Conveniently represents elastic search results in a more compact fashion

Available properties:

Public Class Methods

new(raw) click to toggle source
# File lib/stretcher/search_results.rb, line 11
def initialize(raw)
  @raw_plain = raw
end

Public Instance Methods

docs()
Alias for: documents
documents() click to toggle source

Returns a ‘prettier’ version of elasticsearch results Also aliased as docs This will:

  1. Return either ‘_source’ or ‘fields’ as the base of the result

  2. Merge any keys beginning with a ‘_’ into it as well (such as ‘_score’)

  3. 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
Also aliased as: docs
facets() click to toggle source

Returns the facet data from elasticsearch Equivalent to raw

# File lib/stretcher/search_results.rb, line 34
def facets
  @facets ||= raw[:facets]
end
raw() click to toggle source

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
raw_plain() click to toggle source

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
results() click to toggle source

DEPRECATED! Call documents instead!

# File lib/stretcher/search_results.rb, line 59
def results
  documents
end
total() click to toggle source

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

copy_highlight(hit, doc) click to toggle source
# File lib/stretcher/search_results.rb, line 87
def copy_highlight(hit, doc)
  if highlight = hit["highlight"]
    doc[:_highlight] = highlight
  end
  doc
end
copy_underscores(hit, doc) click to toggle source
# 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
extract_source(hit) click to toggle source
# 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