Class: Queries::GeographicArea::Autocomplete

Inherits:
Query::Autocomplete show all
Includes:
Concerns::AlternateValues, Concerns::Tags
Defined in:
lib/queries/geographic_area/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, #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, #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, **params) ⇒ Autocomplete

Returns a new instance of Autocomplete.



8
9
10
11
12
# File 'lib/queries/geographic_area/autocomplete.rb', line 8

def initialize(string, **params)
  set_tags_params(params)
  set_alternate_value(params)
  super
end

Instance Method Details

#autocompleteObject

Returns Array.

Returns:

  • Array



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/queries/geographic_area/autocomplete.rb', line 25

def autocomplete
  #t = Time.now
  pr_id = project_id.join(',') if project_id.present?

  return [] if query_string.blank?
  queries = [
    [ ::GeographicArea.where(id: query_string).all, false ],
    [ ::GeographicArea.where(name: query_string).all, false ],
    [ ::GeographicArea.joins(:alternate_values).where('alternate_values.value ILIKE ?', query_string).all, false ],
    [ ::GeographicArea.joins(parent_child_join).where(Arel.sql(parent_child_where.to_sql)).limit(5).all, true ],
    [ ::GeographicArea.where(Arel.sql(where_sql)).limit(dynamic_limit).all, true ],
    [ autocomplete_exact_id, false ],
    [ autocomplete_identifier_cached_exact, false ],
    [ autocomplete_identifier_identifier_exact, false ],
    [ autocomplete_alternate_values_name.limit(dynamic_limit), true ]
  ]

  queries.compact!

  updated_queries = []

  queries.each_with_index do |q, i|
    a = q[0]
    if q[1] && query_string.length > 3 # do not use extended query for identifiers
      if project_id.present? && !a.nil?
         a = a.left_outer_joins(:asserted_distributions)
           .left_outer_joins(:collecting_events)
           .select("geographic_areas.*, (COUNT(collecting_events.id) + COUNT(asserted_distributions.id)) AS use_count, CASE WHEN asserted_distributions.project_id IN (#{pr_id}) THEN asserted_distributions.project_id WHEN collecting_events.project_id IN (#{pr_id}) THEN collecting_events.project_id ELSE NULL END AS in_project")
           .group('geographic_areas.id, collecting_events.project_id, asserted_distributions.project_id')
           .order('in_project, use_count DESC')
      end
    end
    updated_queries[i] = a
  end
  result = []
  updated_queries.compact!
  #t2 = Time.now.to_f - t.to_f
  #puts t2
  updated_queries.each do |q|
    result += q.includes(:geographic_areas_geographic_items).to_a
    result.uniq!
    break if result.count > 19
  end
  #t2 = Time.now.to_f - t.to_f
  #puts t2
  result = result[0..19]
end

#autocomplete_alternate_values_nameObject



20
21
22
# File 'lib/queries/geographic_area/autocomplete.rb', line 20

def autocomplete_alternate_values_name
  matching_alternate_value_on(:name)
end

#where_sqlString

TODO: use or_clauses

Returns:

  • (String)


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

def where_sql
  named.to_sql
end