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

Constant Summary collapse

DEFAULT_LIMIT =
40

Constants inherited from Query::Autocomplete

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

Instance Attribute Summary collapse

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 included from Helpers

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

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

Returns a new instance of Autocomplete.

Parameters:

  • string (String)


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

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

Instance Attribute Details

#georeferences ⇒ Boolean?

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



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

def georeferences
  @georeferences
end

Instance Method Details

#autocomplete ⇒ Array

Returns TODO: optimize limits.

Returns:

  • (Array) —

    TODO: optimize limits



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
127
# File 'lib/queries/collecting_event/autocomplete.rb', line 85

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] = apply_restriction(a)
  end

  result = []
  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count >= limit
  end
  result.first(limit)
end

#autocomplete_matching_collectors ⇒ Object



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

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



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

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_wildcard ⇒ Object



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

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_date ⇒ Object



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

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_match ⇒ Object



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

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

#autocomplete_verbatim_habitat ⇒ Object



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

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_md5 ⇒ Object



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

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_longitude ⇒ Object



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

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_end ⇒ Object



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

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_year ⇒ Object



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

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_restriction ⇒ Scope

Returns:

  • (Scope)


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

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