Class: Queries::BiologicalAssociation::Autocomplete

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

Instance Attribute Summary

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, #cached_facet, #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, #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) ⇒ Autocomplete

Returns a new instance of Autocomplete.



5
6
7
# File 'lib/queries/biological_association/autocomplete.rb', line 5

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#anatomical_part_autocompleteQueries::AnatomicalPart::Autocomplete



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

def anatomical_part_autocomplete
  @anatomical_part_autocomplete ||= Queries::AnatomicalPart::Autocomplete
    .new(query_string, project_id: project_id)
end

#anatomical_part_matches(anatomical_part_query, side, results_allowed) ⇒ Object



72
73
74
75
# File 'lib/queries/biological_association/autocomplete.rb', line 72

def anatomical_part_matches(anatomical_part_query, side, results_allowed)
  ids = anatomical_part_query.limit(results_allowed).pluck(:id)
  joined_matches('anatomical_parts', 'AnatomicalPart', side, ids)
end

#autocompleteArray

Returns:

  • (Array)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/queries/biological_association/autocomplete.rb', line 117

def autocomplete
  result = []
  ordered_lazy_queries.each do |q|
    remaining = 50 - result.count
    break if remaining <= 0

    result += q.call(remaining)
    result.uniq!
  end

  result[0..49]
end

#biological_relationship_autocompleteQueries::BiologicalRelationship::Autocomplete



28
29
30
31
# File 'lib/queries/biological_association/autocomplete.rb', line 28

def biological_relationship_autocomplete
  @biological_relationship_autocomplete ||= Queries::BiologicalRelationship::Autocomplete
    .new(query_string, project_id: project_id)
end

#biological_relationship_matches(results_allowed) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/queries/biological_association/autocomplete.rb', line 77

def biological_relationship_matches(results_allowed)
  ids = biological_relationship_autocomplete.all.limit(results_allowed).pluck(:id)
  return [] if ids.empty?

  ::BiologicalAssociation
    .joins(:biological_relationship)
    .where(biological_relationship: { id: ids })
    .to_a
end

#collection_object_autocompleteQueries::CollectionObject::Autocomplete



16
17
18
19
# File 'lib/queries/biological_association/autocomplete.rb', line 16

def collection_object_autocomplete
  @collection_object_autocomplete ||= Queries::CollectionObject::Autocomplete
    .new(query_string, project_id: project_id)
end

#collection_object_matches(collection_object_query, side, results_allowed) ⇒ Object



62
63
64
65
# File 'lib/queries/biological_association/autocomplete.rb', line 62

def collection_object_matches(collection_object_query, side, results_allowed)
  ids = collection_object_query.limit(results_allowed).pluck(:id)
  joined_matches('collection_objects', 'CollectionObject', side, ids)
end

#field_occurrence_autocompleteQueries::FieldOccurrence::Autocomplete



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

def field_occurrence_autocomplete
  @field_occurrence_autocomplete ||= Queries::FieldOccurrence::Autocomplete
    .new(query_string, project_id: project_id)
end

#field_occurrence_matches(field_occurrence_query, side, results_allowed) ⇒ Object



67
68
69
70
# File 'lib/queries/biological_association/autocomplete.rb', line 67

def field_occurrence_matches(field_occurrence_query, side, results_allowed)
  ids = field_occurrence_query.limit(results_allowed).pluck(:id)
  joined_matches('field_occurrences', 'FieldOccurrence', side, ids)
end

#joined_matches(related_table_name, related_type, side, ids) ⇒ Array<BiologicalAssociation>

Returns biological_associations where the subject or object (on side) is one of the related_klass records identified by ids.

Returns:

  • (Array<BiologicalAssociation>)

    biological_associations where the subject or object (on side) is one of the related_klass records identified by ids



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/queries/biological_association/autocomplete.rb', line 42

def joined_matches(related_table_name, related_type, side, ids)
  return [] if ids.empty?

  foreign_key_column = "biological_association_#{side}_id"
  type_column = "biological_association_#{side}_type"

  base_query
    .joins(
      "JOIN #{related_table_name} ON biological_associations.#{foreign_key_column} = #{related_table_name}.id " \
      "AND biological_associations.#{type_column} = '#{related_type}'"
    )
    .where(related_table_name.to_sym => { id: ids })
    .to_a
end

#ordered_lazy_queriesArray<Proc>

Returns An ordered list of thunks, highest priority first. Each, when called with the number of results still wanted, lazily runs its own query and returns an Array. Kept as thunks (rather than eagerly building/running every branch, as this used to) so autocomplete can stop pulling further branches, and cap how many candidate ids a branch even asks for, once enough results are found.

Returns:

  • (Array<Proc>)

    An ordered list of thunks, highest priority first. Each, when called with the number of results still wanted, lazily runs its own query and returns an Array. Kept as thunks (rather than eagerly building/running every branch, as this used to) so autocomplete can stop pulling further branches, and cap how many candidate ids a branch even asks for, once enough results are found.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/queries/biological_association/autocomplete.rb', line 93

def ordered_lazy_queries
  queries = [->(n) { [autocomplete_exact_id].compact.flat_map(&:to_a) }]

  queries << ->(n) { otu_matches(:subject, n) }
  queries << ->(n) { otu_matches(:object, n) }

  co = collection_object_autocomplete.base_queries
  co.each { |q| queries << ->(n) { collection_object_matches(q, :subject, n) } }
  co.each { |q| queries << ->(n) { collection_object_matches(q, :object, n) } }

  fo = field_occurrence_autocomplete.base_queries
  fo.each { |q| queries << ->(n) { field_occurrence_matches(q, :subject, n) } }
  fo.each { |q| queries << ->(n) { field_occurrence_matches(q, :object, n) } }

  queries << ->(n) { biological_relationship_matches(n) }

  ap = anatomical_part_autocomplete.updated_queries
  ap.each { |q| queries << ->(n) { anatomical_part_matches(q, :subject, n) } }
  ap.each { |q| queries << ->(n) { anatomical_part_matches(q, :object, n) } }

  queries
end

#otu_autocompleteQueries::Otu::Autocomplete



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

def otu_autocomplete
  @otu_autocomplete ||= Queries::Otu::Autocomplete
    .new(query_string, project_id: project_id)
end

#otu_matches(side, results_allowed) ⇒ Object



57
58
59
60
# File 'lib/queries/biological_association/autocomplete.rb', line 57

def otu_matches(side, results_allowed)
  ids = otu_autocomplete.autocomplete_base.limit(results_allowed).pluck(:id)
  joined_matches('otus', 'Otu', side, ids)
end