Class: Queries::CollectingEvent::Autocomplete

Inherits:
Query::Autocomplete show all
Includes:
Queries::Concerns::DateRanges, Queries::Concerns::Roles, Helpers
Defined in:
lib/queries/collecting_event/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 included from Helpers

#boolean_param, #integer_param, #negate_facet, #split_pairs, #split_repeated_pairs, #tri_value_array

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, georeferences: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.



17
18
19
20
# File 'lib/queries/collecting_event/autocomplete.rb', line 17

def initialize(string, project_id: nil, georeferences: nil)
  @georeferences = boolean_param({georeferences:}, :georeferences)
  super(string, project_id:)
end

Instance Attribute Details

#georeferencesBoolean?

Returns true - only collecting events with a georeference false - only collecting events without a georeference nil - no restriction.

Returns:

  • (Boolean, nil)

    true - only collecting events with a georeference false - only collecting events without a georeference nil - no restriction



13
14
15
# File 'lib/queries/collecting_event/autocomplete.rb', line 13

def georeferences
  @georeferences
end

Instance Method Details

#autocompleteArray

Returns TODO: optimize limits.

Returns:

  • (Array)

    TODO: optimize limits



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/queries/collecting_event/autocomplete.rb', line 84

def autocomplete
  return [] if query_string.blank?
  queries = [
    autocomplete_exact_id,
    autocomplete_verbatim_label_md5,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_exact,
    autocomplete_identifier_cached_like.limit(4),
    autocomplete_verbatim_field_number_match,
    autocomplete_start_or_end_date,
    autocomplete_start_date_wild_card(:verbatim_locality),
    autocomplete_start_date_wild_card(:cached),
    autocomplete_matching_collectors,
    autocomplete_verbatim_latitude_or_longitude,
    autocomplete_verbatim_collectors_wildcard,
    autocomplete_verbatim_date,
    autocomplete_verbatim_habitat,
    autocomplete_verbatim_locality_wildcard_end,
    autocomplete_verbatim_locality_wildcard_end_starting_year,

    autocomplete_cached_wildcard_anywhere&.limit(20),
  ]

  queries.compact!

  return [] if queries.empty?
  updated_queries = []

  queries.each_with_index do |q ,i|
    a = q.where(project_id:) if project_id.present?
    a ||= q
    a = a.merge(georeference_restriction) unless georeferences.nil?
    updated_queries[i] = a
  end

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

#autocomplete_matching_collectorsObject



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

def autocomplete_matching_collectors
  return nil if no_terms?
  matching_person_cached(:collector).limit(20)
end

#autocomplete_start_date_wild_card(field = :verbatim_locality) ⇒ Object



33
34
35
36
37
38
# File 'lib/queries/collecting_event/autocomplete.rb', line 33

def autocomplete_start_date_wild_card(field = :verbatim_locality)
  a = with_start_date
  b = string_fragments
  return nil if a.nil? || b.empty? || field.nil?
  base_query.where( a.and(table[field].matches(b.join)).to_sql).limit(20)
end

#autocomplete_verbatim_collectors_wildcardObject



44
45
46
47
# File 'lib/queries/collecting_event/autocomplete.rb', line 44

def autocomplete_verbatim_collectors_wildcard
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_collectors].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_dateObject



54
55
56
57
# File 'lib/queries/collecting_event/autocomplete.rb', line 54

def autocomplete_verbatim_date
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_date].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_field_number_matchObject



40
41
42
# File 'lib/queries/collecting_event/autocomplete.rb', line 40

def autocomplete_verbatim_field_number_match
  base_query.where( table[:verbatim_field_number].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_habitatObject



59
60
61
62
# File 'lib/queries/collecting_event/autocomplete.rb', line 59

def autocomplete_verbatim_habitat
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_habitat].matches(start_and_end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_label_md5Object



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

def autocomplete_verbatim_label_md5
  return nil if query_string.to_s.length < 4
  md5 = Utilities::Strings.generate_md5(query_string)
  base_query.where( table[:md5_of_verbatim_label].eq(md5)).limit(3)
end

#autocomplete_verbatim_latitude_or_longitudeObject



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

def autocomplete_verbatim_latitude_or_longitude
  return nil if query_string.nil?
  base_query.where(
    table[:verbatim_latitude].matches(start_and_end_wildcard)
    .or(table[:verbatim_longitude].matches(start_and_end_wildcard)).to_sql
  ).limit(20)
end

#autocomplete_verbatim_locality_wildcard_endObject



49
50
51
52
# File 'lib/queries/collecting_event/autocomplete.rb', line 49

def autocomplete_verbatim_locality_wildcard_end
  return nil if query_string.length < 4
  base_query.where( table[:verbatim_locality].matches(end_wildcard).to_sql).limit(20)
end

#autocomplete_verbatim_locality_wildcard_end_starting_yearObject



72
73
74
75
76
77
78
79
80
# File 'lib/queries/collecting_event/autocomplete.rb', line 72

def autocomplete_verbatim_locality_wildcard_end_starting_year
  a = years
  return nil if query_string.length < 7 || a.empty?
  base_query.where(
    table[:start_date_year].in(a).
    and( table[:verbatim_locality].matches(string_fragments.join))
    .to_sql)
    .limit(20)
end

#georeference_restrictionScope

Returns:

  • (Scope)


129
130
131
# File 'lib/queries/collecting_event/autocomplete.rb', line 129

def georeference_restriction
  georeferences ? ::CollectingEvent.with_georeferences : ::CollectingEvent.without_georeferences
end