Class: Stretcher::SearchResults
- Inherits:
-
Object
- Object
- Stretcher::SearchResults
- 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
-
#documents ⇒ Object
(also: #docs)
Returns a ‘prettier’ version of elasticsearch results Also aliased as
docs
This will:. -
#facets ⇒ Object
Returns the facet data from elasticsearch Equivalent to raw.
-
#initialize(raw) ⇒ SearchResults
constructor
A new instance of SearchResults.
-
#raw ⇒ Object
Returns a Hashie::Mash version of the raw response.
-
#raw_plain ⇒ Object
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.
-
#results ⇒ Object
DEPRECATED! Call #documents instead!.
-
#total ⇒ Object
Returns the total number of results.
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
#documents ⇒ Object Also known as: docs
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’
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 |
#facets ⇒ Object
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 |
#raw ⇒ Object
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_plain ⇒ Object
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 |
#results ⇒ Object
DEPRECATED! Call #documents instead!
59 60 61 |
# File 'lib/stretcher/search_results.rb', line 59 def results documents end |
#total ⇒ Object
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 |