Class: TypesenseModel::SearchResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/typesense_model/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_responseObject (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

#countObject

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

#facetsObject



105
106
107
# File 'lib/typesense_model/search.rb', line 105

def facets
  @raw_response['facet_counts'] || []
end

#grouped_hitsArray<Hash>

Grouped results, populated only when the search used ‘group_by`.

Returns:

  • (Array<Hash>)

    { group_key:, hits: [records] }



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

#hitsObject



56
57
58
# File 'lib/typesense_model/search.rb', line 56

def hits
  @raw_response['hits'] || []
end

#hits_with_metaArray<Hash>

Each hit paired with its search metadata: the model record, the per-field highlight snippets, and the relevance score Typesense computed.

Returns:

  • (Array<Hash>)

    { record:, highlights:, highlight:, text_match: }



64
65
66
67
68
69
70
71
72
73
# File 'lib/typesense_model/search.rb', line 64

def hits_with_meta
  hits.map do |hit|
    {
      record: @model_class.new(hit['document']),
      highlights: hit['highlights'] || [],
      highlight: hit['highlight'] || {},
      text_match: hit['text_match']
    }
  end
end

#limitObject



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

#offsetObject



98
99
100
# File 'lib/typesense_model/search.rb', line 98

def offset(*)
  self
end

#sizeObject



87
88
89
# File 'lib/typesense_model/search.rb', line 87

def size
  total_hits
end

#total_hitsObject



91
92
93
# File 'lib/typesense_model/search.rb', line 91

def total_hits
  @raw_response['found'] || 0
end