Class: Stretcher::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
lib/stretcher/search_results.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ SearchResults

Returns a new instance of SearchResults



11
12
13
# File 'lib/stretcher/search_results.rb', line 11

def initialize(raw)
  @raw_plain = raw
end

Instance Method Details

#documentsObject Also known as: docs

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'



46
47
48
49
50
51
52
53
54
# 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

#facetsObject

Returns the facet data from elasticsearch Equivalent to raw



34
35
36
# File 'lib/stretcher/search_results.rb', line 34

def facets
  @facets ||= raw[:facets]
end

#rawObject

Returns a Hashie::Mash version of the raw response



23
24
25
# File 'lib/stretcher/search_results.rb', line 23

def raw
  @raw ||= Hashie::Mash.new(@raw_plain)
end

#raw_plainObject

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.



18
19
20
# File 'lib/stretcher/search_results.rb', line 18

def raw_plain
  @raw_plain
end

#resultsObject

DEPRECATED! Call #documents instead!



59
60
61
# File 'lib/stretcher/search_results.rb', line 59

def results
  documents
end

#totalObject

Returns the total number of results



28
29
30
# File 'lib/stretcher/search_results.rb', line 28

def total
  @total ||= raw_plain['hits']['total']
end