Class: Queries::AssertedDistribution::Autocomplete

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

Constant Summary collapse

DEFAULT_LIMIT =
50

Constants inherited from Query::Autocomplete

Query::Autocomplete::DELEGATED_DEPTH, Query::Autocomplete::DELEGATED_MINIMUM_LENGTH, Query::Autocomplete::LITERAL_RESTRICTION_MAX

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#delegated_results, #dynamic_limit, #limit, #project_id, #query_string, #restrict_to

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#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.



7
8
9
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 7

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:

  • (Array)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 114

def autocomplete
  # Method names, built in turn, so that later queries (e.g. the
  # biological association one, which runs the BA autocomplete) don't
  # run once the results are filled
  queries = %i[
    autocomplete_matching_source
    autocomplete_matching_otu_name
    autocomplete_matching_taxon_name
    autocomplete_biological_association
    autocomplete_observation_otu_name
    autocomplete_observation_taxon_name
    autocomplete_conveyance_otu_name
    autocomplete_conveyance_taxon_name
    autocomplete_depiction_otu_name
    autocomplete_depiction_taxon_name
    autocomplete_matching_geographic_area
    autocomplete_matching_gazetteer
    autocomplete_biological_associations_graph
  ]

  result = []
  queries.each do |m|
    q = send(m)
    next if q.nil?

    result += apply_restriction(q.where(asserted_distributions: {project_id:})).to_a
    result.uniq!
    break if result.count >= limit
  end
  result.first(limit)
end

#autocomplete_biological_association ⇒ Object



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

def autocomplete_biological_association
  asserted_object_autocomplete(
    object_type: 'BiologicalAssociation',
    object_autocomplete_class: Queries::BiologicalAssociation::Autocomplete,
    object_association: :asserted_distribution_object
  )
end

#autocomplete_biological_associations_graph ⇒ Object



51
52
53
54
55
56
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 51

def autocomplete_biological_associations_graph
  # TODO: should search on BAs of graphs as well.
  base_query
    .with_biological_associations_graphs
    .where( biological_associations_graph_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_conveyance_otu_name ⇒ Object



58
59
60
61
62
63
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 58

def autocomplete_conveyance_otu_name
  base_query
    .with_otu_conveyances
    .joins('JOIN otus ON otus.id = conveyances.conveyance_object_id')
    .where( otu_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_conveyance_taxon_name ⇒ Object



65
66
67
68
69
70
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 65

def autocomplete_conveyance_taxon_name
  base_query
    .with_otu_conveyances
    .joins('JOIN otus ON otus.id = conveyances.conveyance_object_id JOIN taxon_names ON taxon_names.id = otus.taxon_name_id')
    .where( taxon_name_table[:cached].matches(query_string + '%')   ).limit(20)
end

#autocomplete_depiction_otu_name ⇒ Object



72
73
74
75
76
77
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 72

def autocomplete_depiction_otu_name
  base_query
    .with_otu_depictions
    .joins('JOIN otus ON otus.id = depictions.depiction_object_id')
    .where( otu_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_depiction_taxon_name ⇒ Object



79
80
81
82
83
84
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 79

def autocomplete_depiction_taxon_name
  base_query
    .with_otu_depictions
    .joins('JOIN otus ON otus.id = depictions.depiction_object_id JOIN taxon_names ON taxon_names.id = otus.taxon_name_id')
    .where( taxon_name_table[:cached].matches(query_string + '%')   ).limit(20)
end

#autocomplete_matching_gazetteer ⇒ Object



104
105
106
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 104

def autocomplete_matching_gazetteer
  base_query.joins("JOIN gazetteers ON asserted_distributions.asserted_distribution_shape_type = 'Gazetteer' AND asserted_distribution_shape_id = gazetteers.id").where( gazetteer_table[:name].matches(query_string + '%')   ).limit(50)
end

#autocomplete_matching_geographic_area ⇒ Object



100
101
102
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 100

def autocomplete_matching_geographic_area
  base_query.joins("JOIN geographic_areas ON asserted_distributions.asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id = geographic_areas.id").where( geographic_area_table[:name].matches(query_string + '%')   ).limit(50)
end

#autocomplete_matching_otu_name ⇒ Object



35
36
37
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 35

def autocomplete_matching_otu_name
  base_query.with_otus.where( otu_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_matching_source ⇒ Object

Dubious use



109
110
111
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 109

def autocomplete_matching_source
  base_query.joins(:sources).where( source_table[:cached].matches('%' + query_string + '%') ).limit(5)
end

#autocomplete_matching_taxon_name ⇒ Object



39
40
41
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 39

def autocomplete_matching_taxon_name
  base_query.with_taxon_names.where( taxon_name_table[:cached].matches(query_string + '%')   ).limit(20)
end

#autocomplete_observation_otu_name ⇒ Object



86
87
88
89
90
91
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 86

def autocomplete_observation_otu_name
  base_query
    .with_otu_observations
    .joins('JOIN otus ON otus.id = observations.observation_object_id')
    .where( otu_table[:name].matches(query_string + '%')   ).limit(20)
end

#autocomplete_observation_taxon_name ⇒ Object



93
94
95
96
97
98
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 93

def autocomplete_observation_taxon_name
  base_query
    .with_otu_observations
    .joins('JOIN otus ON otus.id = observations.observation_object_id JOIN taxon_names ON taxon_names.id = otus.taxon_name_id')
    .where( taxon_name_table[:cached].matches(query_string + '%')   ).limit(20)
end

#biological_associations_graph_table ⇒ Object



19
20
21
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 19

def biological_associations_graph_table
  ::BiologicalAssociationsGraph.arel_table
end

#gazetteer_table ⇒ Object



27
28
29
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 27

def gazetteer_table
  ::Gazetteer.arel_table
end

#geographic_area_table ⇒ Object



23
24
25
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 23

def geographic_area_table
  ::GeographicArea.arel_table
end

#otu_table ⇒ Object



11
12
13
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 11

def otu_table
  ::Otu.arel_table
end

#source_table ⇒ Object



31
32
33
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 31

def source_table
  ::Source.arel_table
end

#taxon_name_table ⇒ Object



15
16
17
# File 'lib/queries/asserted_distribution/autocomplete.rb', line 15

def taxon_name_table
  ::TaxonName.arel_table
end