Class: TypesenseModel::SearchResults
- Inherits:
-
Object
- Object
- TypesenseModel::SearchResults
- Includes:
- Enumerable
- Defined in:
- lib/typesense_model/search.rb
Instance Attribute Summary collapse
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
Instance Method Summary collapse
-
#count ⇒ Object
PAGY COMPATIBILITY.
- #each(&block) ⇒ Object
-
#facet(field_name) ⇒ Object
Get a specific facet by field name.
-
#facet_values(field_name) ⇒ Object
Get facet values for a specific field.
- #facets ⇒ Object
-
#grouped_hits ⇒ Array<Hash>
Grouped results, populated only when the search used ‘group_by`.
- #hits ⇒ Object
-
#hits_with_meta ⇒ Array<Hash>
Each hit paired with its search metadata: the model record, the per-field highlight snippets, and the relevance score Typesense computed.
-
#initialize(response, model_class) ⇒ SearchResults
constructor
A new instance of SearchResults.
- #limit ⇒ Object
- #map(&block) ⇒ Object
- #offset ⇒ Object
- #size ⇒ Object
- #total_hits ⇒ Object
Constructor Details
#initialize(response, model_class) ⇒ SearchResults
Returns a new instance of SearchResults.
39 40 41 42 |
# File 'lib/typesense_model/search.rb', line 39 def initialize(response, model_class) @raw_response = response @model_class = model_class end |
Instance Attribute Details
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
37 38 39 |
# File 'lib/typesense_model/search.rb', line 37 def raw_response @raw_response end |
Instance Method Details
#count ⇒ Object
PAGY COMPATIBILITY
95 96 97 |
# File 'lib/typesense_model/search.rb', line 95 def count(*) total_hits end |
#each(&block) ⇒ Object
44 45 46 47 48 |
# File 'lib/typesense_model/search.rb', line 44 def each(&block) hits.each do |hit| yield @model_class.new(hit['document']) end end |
#facet(field_name) ⇒ Object
Get a specific facet by field name
110 111 112 |
# File 'lib/typesense_model/search.rb', line 110 def facet(field_name) facets.find { |f| f['field_name'] == field_name.to_s } end |
#facet_values(field_name) ⇒ Object
Get facet values for a specific field
115 116 117 |
# File 'lib/typesense_model/search.rb', line 115 def facet_values(field_name) facet(field_name)&.fetch('counts', []) || [] end |
#facets ⇒ Object
105 106 107 |
# File 'lib/typesense_model/search.rb', line 105 def facets @raw_response['facet_counts'] || [] end |
#grouped_hits ⇒ Array<Hash>
Grouped results, populated only when the search used ‘group_by`.
78 79 80 81 82 83 84 85 |
# File 'lib/typesense_model/search.rb', line 78 def grouped_hits (@raw_response['grouped_hits'] || []).map do |group| { group_key: group['group_key'], hits: (group['hits'] || []).map { |h| @model_class.new(h['document']) } } end end |
#hits ⇒ Object
56 57 58 |
# File 'lib/typesense_model/search.rb', line 56 def hits @raw_response['hits'] || [] end |
#hits_with_meta ⇒ Array<Hash>
Each hit paired with its search metadata: the model record, the per-field highlight snippets, and the relevance score Typesense computed.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/typesense_model/search.rb', line 64 def hits.map do |hit| { record: @model_class.new(hit['document']), highlights: hit['highlights'] || [], highlight: hit['highlight'] || {}, text_match: hit['text_match'] } end end |
#limit ⇒ Object
101 102 103 |
# File 'lib/typesense_model/search.rb', line 101 def limit(*) self end |
#map(&block) ⇒ Object
50 51 52 53 54 |
# File 'lib/typesense_model/search.rb', line 50 def map(&block) hits.map do |hit| block.call(@model_class.new(hit['document'])) end end |
#offset ⇒ Object
98 99 100 |
# File 'lib/typesense_model/search.rb', line 98 def offset(*) self end |
#size ⇒ Object
87 88 89 |
# File 'lib/typesense_model/search.rb', line 87 def size total_hits end |
#total_hits ⇒ Object
91 92 93 |
# File 'lib/typesense_model/search.rb', line 91 def total_hits @raw_response['found'] || 0 end |