Class: Autoselect::TaxonName::Levels::Fast

Inherits:
Level
  • Object
show all
Defined in:
lib/autoselect/taxon_name/levels/fast.rb

Overview

Fast level: prefix-only match on the cached column via direct Arel. Goal: satisfy 80%+ of queries with minimal DB overhead (no GIN, no similarity).

Constant Summary

Constants inherited from Level

Level::DEFAULT_FUSE_MS, Level::EXTERNAL_FUSE_MS, Level::MINIMUM_RESULTS

Instance Method Summary collapse

Methods inherited from Level

#external?, #fuse_ms, #metadata, #minimum_results, #model_key, #record_info, #record_info_html, #record_label, #record_label_html

Instance Method Details

#call(term:, operator: nil, project_id: nil, user_id: nil, **_kwargs) ⇒ Array

Returns of TaxonName instances.

Parameters:

  • term (String)
  • project_id (Integer, nil) (defaults to: nil)

Returns:

  • (Array)

    of TaxonName instances



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/autoselect/taxon_name/levels/fast.rb', line 24

def call(term:, operator: nil, project_id: nil, user_id: nil, **_kwargs)
  return [] if term.blank?

  sanitized = ::ApplicationRecord.sanitize_sql_like(term)
  t = ::TaxonName.arel_table

  exact  = t[:cached].eq(term)
  prefix = t[:cached].matches("#{sanitized}%")
  clause = exact.or(prefix)

  scope = ::TaxonName.where(clause.to_sql)
  scope = scope.where(project_id:) if project_id.present?
  scope.order(:cached).limit(20).to_a
end

#descriptionObject



17
18
19
# File 'lib/autoselect/taxon_name/levels/fast.rb', line 17

def description
  'Prefix match on cached display name (fastest, no fuzzy matching)'
end

#keyObject



9
10
11
# File 'lib/autoselect/taxon_name/levels/fast.rb', line 9

def key
  :fast
end

#labelObject



13
14
15
# File 'lib/autoselect/taxon_name/levels/fast.rb', line 13

def label
  'Fast'
end