Class: Queries::CollectionObject::Autocomplete
Constant Summary
collapse
- DEFAULT_LIMIT =
40
Query::Autocomplete::DELEGATED_DEPTH, Query::Autocomplete::DELEGATED_MINIMUM_LENGTH, Query::Autocomplete::LITERAL_RESTRICTION_MAX
Instance Attribute Summary
#delegated_results, #dynamic_limit, #limit, #project_id, #query_string, #restrict_to
Attributes inherited from Query
#query_string, #terms
Instance Method Summary
collapse
#apply_restriction, #asserted_object_autocomplete, #asserted_object_restriction, #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, #cached_facet, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #delegated_autocomplete, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #order_by_id_rank, #parent, #parent_child_join, #parent_child_where, #pieces, #safe_integers, #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, restrict_to: nil, limit: nil) ⇒ Autocomplete
Returns a new instance of Autocomplete.
9
10
11
|
# File 'lib/queries/collection_object/autocomplete.rb', line 9
def initialize(string, project_id: nil, restrict_to: nil, limit: nil)
super(string, project_id:, restrict_to:, limit:)
end
|
Instance Method Details
#autocomplete ⇒ Array
Returns TODO: optimize limits.
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/queries/collection_object/autocomplete.rb', line 87
def autocomplete
return [] if query_string.blank?
updated_queries = base_queries
result = []
updated_queries.each do |q|
result += q.to_a
result.uniq!
break if result.count >= limit
end
result.first(limit)
end
|
#autocomplete_in_container_by_identifier ⇒ Object
54
55
56
57
58
|
# File 'lib/queries/collection_object/autocomplete.rb', line 54
def autocomplete_in_container_by_identifier
q = identifier_table[:identifier].matches('%' + query_string + '%')
::CollectionObject.joins(container: [:identifiers])
.where(q.to_sql)
end
|
#autocomplete_in_container_by_identifier_cached_exact ⇒ Object
48
49
50
51
52
|
# File 'lib/queries/collection_object/autocomplete.rb', line 48
def autocomplete_in_container_by_identifier_cached_exact
q = identifier_table[:cached].eq(query_string)
::CollectionObject.joins(container: [:identifiers])
.where(q.to_sql)
end
|
#autocomplete_otu_determined_as ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/queries/collection_object/autocomplete.rb', line 39
def autocomplete_otu_determined_as
t = otu_table[:name].matches_any(terms)
::CollectionObject.
joins(taxon_determinations: [:otu]).
where(t.to_sql).references(:taxon_determinations, :otus).
order('otus.name ASC').limit(10)
end
|
#autocomplete_taxon_name_determined_as ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/queries/collection_object/autocomplete.rb', line 28
def autocomplete_taxon_name_determined_as
t = taxon_name_table[:name].matches_any(terms).or(taxon_name_table[:cached].matches_any(terms) )
::CollectionObject.
includes(:identifiers, taxon_determinations: [otu: :taxon_name]).
joins(taxon_determinations: [:otu]).
where(t.to_sql).
references(:taxon_determinations, :otus, :taxon_names).
order('otus.name ASC').limit(10)
end
|
#base_queries ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/queries/collection_object/autocomplete.rb', line 60
def base_queries
queries = [
autocomplete_exact_id,
autocomplete_identifier_cached_exact,
autocomplete_in_container_by_identifier_cached_exact,
autocomplete_identifier_identifier_exact,
autocomplete_in_container_by_identifier,
autocomplete_taxon_name_determined_as,
autocomplete_otu_determined_as,
autocomplete_identifier_cached_like,
]
queries.compact!
return [] if queries.nil?
updated_queries = []
queries.each_with_index do |q ,i|
a = q.where(project_id: project_id) if project_id.present?
a ||= q
updated_queries[i] = apply_restriction(a)
end
updated_queries
end
|
#containers_table ⇒ Arel::Table
24
25
26
|
# File 'lib/queries/collection_object/autocomplete.rb', line 24
def containers_table
::Container.arel_table
end
|
#otu_table ⇒ Arel::Table
19
20
21
|
# File 'lib/queries/collection_object/autocomplete.rb', line 19
def otu_table
::Otu.arel_table
end
|
#taxon_name_table ⇒ Arel::Table
14
15
16
|
# File 'lib/queries/collection_object/autocomplete.rb', line 14
def taxon_name_table
::TaxonName.arel_table
end
|