Class: Parse::AtlasSearch::FacetedResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/parse/atlas_search/result.rb

Overview

Result container for faceted search operations. Provides results, facet counts, and total count.

Examples:

Using facets

result = Parse::AtlasSearch.faceted_search("Song", "rock", facets)
result.facets[:genre].each do |bucket|
  puts "#{bucket[:value]}: #{bucket[:count]}"
end

Total count

puts "Total matches: #{result.total_count}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results:, facets:, total_count:) ⇒ FacetedResult

Returns a new instance of FacetedResult.

Parameters:

  • results (Array)

    the search results

  • facets (Hash)

    the facet results

  • total_count (Integer)

    the total matching document count



157
158
159
160
161
# File 'lib/parse/atlas_search/result.rb', line 157

def initialize(results:, facets:, total_count:)
  @results = results
  @facets = facets
  @total_count = total_count
end

Instance Attribute Details

#facetsHash (readonly)

Returns the facet results with counts Format: { facet_name: [{ value: “value”, count: 123 }, …] }.

Returns:

  • (Hash)

    the facet results with counts Format: { facet_name: [{ value: “value”, count: 123 }, …] }



149
150
151
# File 'lib/parse/atlas_search/result.rb', line 149

def facets
  @facets
end

#resultsArray<Parse::Object> (readonly)

Returns the search results.

Returns:



145
146
147
# File 'lib/parse/atlas_search/result.rb', line 145

def results
  @results
end

#total_countInteger (readonly)

Returns the total number of matching documents.

Returns:

  • (Integer)

    the total number of matching documents



152
153
154
# File 'lib/parse/atlas_search/result.rb', line 152

def total_count
  @total_count
end

Instance Method Details

#countInteger Also known as: size

Returns the number of returned results.

Returns:

  • (Integer)

    the number of returned results



164
165
166
# File 'lib/parse/atlas_search/result.rb', line 164

def count
  @results.size
end

#each {|Object| ... } ⇒ Object

Iterate over results

Yields:

  • (Object)

    each result object



177
178
179
# File 'lib/parse/atlas_search/result.rb', line 177

def each(&block)
  @results.each(&block)
end

#empty?Boolean

Returns true if there are no results.

Returns:

  • (Boolean)

    true if there are no results



171
172
173
# File 'lib/parse/atlas_search/result.rb', line 171

def empty?
  @results.empty?
end

#facet(name) ⇒ Array<Hash>?

Get facet buckets for a specific facet

Parameters:

Returns:

  • (Array<Hash>, nil)

    the facet buckets or nil if facet doesn’t exist



189
190
191
# File 'lib/parse/atlas_search/result.rb', line 189

def facet(name)
  @facets[name.to_sym] || @facets[name.to_s]
end

#facet_namesArray<Symbol>

Returns the available facet names.

Returns:



194
195
196
# File 'lib/parse/atlas_search/result.rb', line 194

def facet_names
  @facets.keys
end

#firstObject?

Returns the first result.

Returns:

  • (Object, nil)

    the first result



182
183
184
# File 'lib/parse/atlas_search/result.rb', line 182

def first
  @results.first
end

#to_aArray

Returns the results as an array.

Returns:

  • (Array)

    the results as an array



199
200
201
# File 'lib/parse/atlas_search/result.rb', line 199

def to_a
  @results.to_a
end