Class: Queries::Extract::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/extract/autocomplete.rb

Instance Attribute Summary collapse

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, #referenced_klass, referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.



12
13
14
# File 'lib/queries/extract/autocomplete.rb', line 12

def initialize(string, project_id: nil)
  super
end

Instance Attribute Details

#taxon_name_idsObject

TaxonName ids that match the query term

Returns:

  • Array !! Not an external param



8
9
10
# File 'lib/queries/extract/autocomplete.rb', line 8

def taxon_name_ids
  @taxon_name_ids
end

Instance Method Details

#autocompleteArray

Returns TODO: optimize limits.

Returns:

  • (Array)

    TODO: optimize limits



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/queries/extract/autocomplete.rb', line 102

def autocomplete
  updated_queries = base_queries

  result = []

  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 100
  end
  result[0..100]
end

#autocomplete_in_container_by_identifierObject



69
70
71
72
73
# File 'lib/queries/extract/autocomplete.rb', line 69

def autocomplete_in_container_by_identifier
  q = identifier_table[:identifier].matches('%' + query_string + '%')
  ::Extract.joins(container: [:identifiers])
    .where(q.to_sql)
end

#autocomplete_in_container_by_identifier_cached_exactObject



63
64
65
66
67
# File 'lib/queries/extract/autocomplete.rb', line 63

def autocomplete_in_container_by_identifier_cached_exact
  q = identifier_table[:cached].eq(query_string)
  ::Extract.joins(container: [:identifiers])
    .where(q.to_sql)
end

#autocomplete_otu_name_determined_asObject

Extracts attached to Specimens determined as OTU by otu name taxon name



42
43
44
45
46
47
48
49
# File 'lib/queries/extract/autocomplete.rb', line 42

def autocomplete_otu_name_determined_as
  t = ::Otu.arel_table[:name].matches_any(terms)
  ::Extract.
    joins(origin_collection_objects: [:otus])
    .where(t.to_sql)
    .order('otus.name ASC')
    .limit(10)
end

#autocomplete_otu_taxon_name_id_determined_asObject

Extracts attached to Otus with taxon name



52
53
54
55
56
57
58
59
60
61
# File 'lib/queries/extract/autocomplete.rb', line 52

def autocomplete_otu_taxon_name_id_determined_as
  return nil if taxon_name_ids.empty?
  t = ::Otu.arel_table[:taxon_name_id].in(taxon_name_ids)

  a = ::Extract
    .joins(origin_collection_objects: [otus: [:taxon_name]] ) # Join taxon names to order by name
    .where(t.to_sql) # .references(:taxon_determinations, :otus).
    .order('taxon_names.cached ASC, otus.name ASC')
    .limit(10)
end

#autocomplete_taxon_name_taxon_name_idArray

Find matching names, get their IDs then use ancestor to get target Extracts

Returns:

  • (Array)

    !! Returns multiple queries !!



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/queries/extract/autocomplete.rb', line 29

def autocomplete_taxon_name_taxon_name_id
  return [] if taxon_name_ids.size > 100

  results = []
  taxon_name_ids.each do |id|
    results.push ::Queries::Extract::Filter.new(
      taxon_name_id: id
    ).all.limit(10)
  end
  results
end

#base_queriesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/queries/extract/autocomplete.rb', line 75

def base_queries
  queries = [
    autocomplete_identifier_cached_exact,
    autocomplete_in_container_by_identifier_cached_exact,
    autocomplete_identifier_identifier_exact,
    autocomplete_exact_id,
    autocomplete_in_container_by_identifier,
    autocomplete_identifier_cached_like,
    autocomplete_otu_taxon_name_id_determined_as, # here on built here
    autocomplete_otu_name_determined_as,
  ] + autocomplete_taxon_name_taxon_name_id

  queries.compact!

  return [] if queries.empty?

  updated_queries = []

  queries.each do |q|
    a = q.where(project_id:) if project_id.present?
    updated_queries.push a
  end
  updated_queries
end