Class: Ragdoll::SearchResult

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ragdoll/search_result.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.analytics(days: 30) ⇒ Object

Analytics for search results



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/ragdoll/search_result.rb', line 51

def self.analytics(days: 30)
  start_date = days.days.ago
  results = where(created_at: start_date..)
  
  {
    total_results: results.count,
    clicked_results: results.where(clicked: true).count,
    click_through_rate: calculate_ctr(results),
    avg_similarity_score: results.average(:similarity_score)&.round(4),
    high_similarity_results: results.where("similarity_score >= 0.8").count,
    low_similarity_results: results.where("similarity_score < 0.5").count,
    rank_performance: rank_click_analysis(results)
  }
end

.rank_click_analysis(results = nil) ⇒ Object

Analyze click performance by result rank



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/ragdoll/search_result.rb', line 67

def self.rank_click_analysis(results = nil)
  results ||= all
  
  results.group(:result_rank)
         .group("clicked")
         .count
         .each_with_object({}) do |((rank, clicked), count), hash|
    hash[rank] ||= { total: 0, clicked: 0 }
    hash[rank][:total] += count
    hash[rank][:clicked] += count if clicked
  end
         .transform_values do |stats|
    stats.merge(
      ctr: stats[:total] > 0 ? (stats[:clicked].to_f / stats[:total] * 100).round(2) : 0.0
    )
  end
end

.top_performing_embeddings(limit: 20) ⇒ Object

Find embeddings that perform well across multiple searches



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/ragdoll/search_result.rb', line 86

def self.top_performing_embeddings(limit: 20)
  joins(:embedding)
    .group(:embedding_id)
    .select(
      "embedding_id",
      "COUNT(*) as appearance_count",
      "AVG(similarity_score) as avg_similarity",
      "COUNT(CASE WHEN clicked THEN 1 END) as click_count",
      "ROUND(COUNT(CASE WHEN clicked THEN 1 END) * 100.0 / COUNT(*), 2) as ctr"
    )
    .having("COUNT(*) > 1")
    .order("avg_similarity DESC, ctr DESC")
    .limit(limit)
end

Instance Method Details

#contentObject

Get the content through the embedding relationship



31
32
33
# File 'app/models/ragdoll/search_result.rb', line 31

def content
  embedding&.content
end

#documentObject

Get the document through the embedding relationship



36
37
38
# File 'app/models/ragdoll/search_result.rb', line 36

def document
  embedding&.embeddable&.document
end

#document_locationObject

Get the document location



46
47
48
# File 'app/models/ragdoll/search_result.rb', line 46

def document_location
  document&.location
end

#document_titleObject

Get the document title



41
42
43
# File 'app/models/ragdoll/search_result.rb', line 41

def document_title
  document&.title
end

#mark_as_clicked!Object

Mark this result as clicked



26
27
28
# File 'app/models/ragdoll/search_result.rb', line 26

def mark_as_clicked!
  update!(clicked: true, clicked_at: Time.current)
end