Class: Queries::Query

Inherits:
Object
  • Object
show all
Includes:
Arel::Nodes, Concerns::Identifiers
Defined in:
lib/queries/query.rb

Direct Known Subclasses

BiologicalRelationship::Autocomplete, CommonName::Filter, ControlledVocabularyTerm::Autocomplete, Descriptor::Autocomplete, Document::Autocomplete, Georeference::Filter, Namespace::Autocomplete, Otu::Autocomplete, Otu::Filter, AssertedDistribution::Autocomplete, CollectingEvent::Autocomplete, CollectionObject::Autocomplete, CollectionObjectFilterQuery, ContainerAutocompleteQuery, DataAttribute::ValueAutocomplete, GeographicArea::Autocomplete, Identifier::Autocomplete, Organization::Autocomplete, Person::Autocomplete, Serial::Autocomplete, Source::Autocomplete, Source::Filter, TaxonDetermination::Filter, TaxonName::Autocomplete, TaxonNameRelationshipsFilterQuery, TypeMaterialAutocompleteQuery, Repository::Autocomplete, User::Autocomplete

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Query) initialize(string, project_id: nil, **keyword_args)

Returns a new instance of Query

Parameters:

  • args (Hash)


32
33
34
35
36
37
# File 'lib/queries/query.rb', line 32

def initialize(string, project_id: nil, **keyword_args)
  @query_string = string
  @options = keyword_args
  @project_id = project_id
  build_terms
end

Instance Attribute Details

- (Integer) dynamic_limit

Returns:

  • (Integer)


29
30
31
# File 'lib/queries/query.rb', line 29

def dynamic_limit
  @dynamic_limit
end

- (Object) options

parameters from keyword_args, used to group and pass along things like annotator params



26
27
28
# File 'lib/queries/query.rb', line 26

def options
  @options
end

- (Object) project_id

Returns the value of attribute project_id



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

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



21
22
23
# File 'lib/queries/query.rb', line 21

def query_string
  @query_string
end

- (Array) terms

Returns:

  • (Array)


59
60
61
# File 'lib/queries/query.rb', line 59

def terms
  @terms
end

Instance Method Details

- (Array) alphabetic_strings

Returns:

  • (Array)


86
87
88
# File 'lib/queries/query.rb', line 86

def alphabetic_strings
  Utilities::Strings.alphabetic_strings(query_string)
end

- (Array) autocomplete

Returns default the autocomplete result to all

Returns:

  • (Array)

    default the autocomplete result to all



311
312
313
# File 'lib/queries/query.rb', line 311

def autocomplete
  all.to_a
end

- (ActiveRecord::Relation) autocomplete_cached

Returns:

  • (ActiveRecord::Relation)


334
335
336
337
338
# File 'lib/queries/query.rb', line 334

def autocomplete_cached
  a = cached
  return nil if a.nil?
  base_query.where(a.to_sql).limit(20)
end

- (ActiveRecord::Relation) autocomplete_cached_wildcard_anywhere

Returns removes years/integers!

Returns:

  • (ActiveRecord::Relation)

    removes years/integers!



327
328
329
330
331
# File 'lib/queries/query.rb', line 327

def autocomplete_cached_wildcard_anywhere
  a = match_wildcard_in_cached
  return nil if a.nil?
  base_query.where(a.to_sql).limit(20)
end

- (ActiveRecord::Relation) autocomplete_exact_id

Returns:

  • (ActiveRecord::Relation)


316
317
318
# File 'lib/queries/query.rb', line 316

def autocomplete_exact_id
  base_query.where(id: query_string).limit(1)
end

- (ActiveRecord::Relation) autocomplete_ordered_wildcard_pieces_in_cached

Returns:

  • (ActiveRecord::Relation)


321
322
323
# File 'lib/queries/query.rb', line 321

def autocomplete_ordered_wildcard_pieces_in_cached
  base_query.where(match_ordered_wildcard_pieces_in_cached.to_sql).limit(5)
end

- (Object) autocomplete_start_date



340
341
342
343
344
# File 'lib/queries/query.rb', line 340

def autocomplete_start_date
  a = with_start_date 
  return nil if a.nil?
  base_query.where(a.to_sql).limit(20)
end

- (Array) build_terms

Ultimately we should replace this concept with full text indexing.

Returns:

  • (Array)

    a reasonable (starting) interpretation of any query string



66
67
68
# File 'lib/queries/query.rb', line 66

def build_terms
  @terms = @query_string.blank? ? [] : [end_wildcard, start_and_end_wildcard]
end

- (ActiveRecord::Relation?) cached

TODO: rename :cached_matches or similar

Returns:

  • (ActiveRecord::Relation, nil)

    cached matches full query string wildcarded



257
258
259
260
261
262
263
# File 'lib/queries/query.rb', line 257

def cached
  if !terms.empty?
    table[:cached].matches_any(terms)
  else
    nil
  end
end

- (Arel::Nodes::Grouping) combine_or_clauses(clauses)

Returns:

  • (Arel::Nodes::Grouping)

Raises:

  • (TaxonWorks::Error)


295
296
297
298
299
300
301
302
303
# File 'lib/queries/query.rb', line 295

def combine_or_clauses(clauses)
  clauses.compact!
  raise TaxonWorks::Error, 'combine_or_clauses called without a clause, ensure at least one exists' unless !clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

- (String) end_wildcard

Returns:

  • (String)


76
77
78
# File 'lib/queries/query.rb', line 76

def end_wildcard
  query_string + '%'
end

- (Arel::Nodes::Matches) exactly_named

Returns:

  • (Arel::Nodes::Matches)


235
236
237
# File 'lib/queries/query.rb', line 235

def exactly_named
  table[:name].eq(query_string) if !query_string.blank?
end

- (Array) fragments

Used in unordered AND searches

Returns:

  • (Array)

    if 1-5 alphabetic_strings, those alphabetic_strings wrapped in wildcards, else none.



119
120
121
122
123
124
125
126
# File 'lib/queries/query.rb', line 119

def fragments
  a = alphabetic_strings
  if a.size > 0 && a.size < 6
    a.collect{|a| "%#{a}%"}
  else
    []
  end
end

- (Array) integers

Returns of strings representing integers

Returns:

  • (Array)

    of strings representing integers



105
106
107
108
# File 'lib/queries/query.rb', line 105

def integers
  return [] if query_string.blank?
  query_string.split(/\s+/).select{|t| Utilities::Strings.is_i?(t)}
end

- (Arel::Nodes::Matches) match_ordered_wildcard_pieces_in_cached

Returns:

  • (Arel::Nodes::Matches)


276
277
278
# File 'lib/queries/query.rb', line 276

def match_ordered_wildcard_pieces_in_cached
  table[:cached].matches(wildcard_pieces)
end

- (Arel::Nodes::Matches) match_wildcard_end_in_cached

match ALL wildcards, but unordered, if 2 - 6 pieces provided

Returns:

  • (Arel::Nodes::Matches)


282
283
284
# File 'lib/queries/query.rb', line 282

def match_wildcard_end_in_cached
  table[:cached].matches(end_wildcard)
end

- (Arel::Nodes::Matches) match_wildcard_in_cached

match ALL wildcards, but unordered, if 2 - 6 pieces provided

Returns:

  • (Arel::Nodes::Matches)


288
289
290
291
292
# File 'lib/queries/query.rb', line 288

def match_wildcard_in_cached
  b = fragments
  return nil if b.empty?
  table[:cached].matches_all(b)
end

- (Arel::Nodes::Matches) named

Returns:

  • (Arel::Nodes::Matches)


230
231
232
# File 'lib/queries/query.rb', line 230

def named
  table[:name].matches_any(terms) if terms.any?
end

- (String) no_digits

Returns:

  • (String)


151
152
153
# File 'lib/queries/query.rb', line 151

def no_digits
  query_string.gsub(/\d/, '').strip
end

- (Query?) only_ids

Returns used in or_clauses, match on id only if integers alone provided.

Returns:

  • (Query, nil)

    used in or_clauses, match on id only if integers alone provided.



195
196
197
198
199
200
201
# File 'lib/queries/query.rb', line 195

def only_ids
  if only_integers?
    with_id
  else
    nil
  end
end

- (Boolean) only_integers?

Returns true if the query string only contains integers

Returns:

  • (Boolean)

    true if the query string only contains integers



112
113
114
# File 'lib/queries/query.rb', line 112

def only_integers?
  !(query_string =~ /[^\d\s]/i) && !integers.empty?
end

- (Arel::Nodes::TableAlias) parent

Returns:

  • (Arel::Nodes::TableAlias)


240
241
242
# File 'lib/queries/query.rb', line 240

def parent
  table.alias
end

- (Scope) parent_child_join

Returns:

  • (Scope)


171
172
173
# File 'lib/queries/query.rb', line 171

def parent_child_join
  table.join(parent).on(table[:parent_id].eq(parent[:id])).join_sources # !! join_sources ftw
end

- (Arel::Nodes::Grouping) parent_child_where

Match at two levels, for example, 'wa te“ will match ”Washington Co., Texas“

Returns:

  • (Arel::Nodes::Grouping)


177
178
179
180
181
# File 'lib/queries/query.rb', line 177

def parent_child_where
  a,b = query_string.split(/\s+/, 2)
  return table[:id].eq(-1) if a.nil? || b.nil?
  table[:name].matches("#{a}%").and(parent[:name].matches("#{b}%"))
end

- (Array) pieces

Returns split on whitespace

Returns:

  • (Array)

    split on whitespace



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

def pieces
  query_string.split(/\s+/)
end

- (Array) result

Returns the results of the query as ActiveRecord objects

Returns:

  • (Array)

    the results of the query as ActiveRecord objects



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

def result
  []
end

- (Scope) scope

stub

Returns:

  • (Scope)


47
48
49
# File 'lib/queries/query.rb', line 47

def scope
  where('1 = 2')
end

- (Date.new?) simple_date

Returns:

  • (Date.new, nil)


204
205
206
207
208
209
210
# File 'lib/queries/query.rb', line 204

def simple_date
  begin
    Date.parse(query_string)
  rescue ArgumentError
    return nil
  end
end

- (String) start_and_end_wildcard

Returns:

  • (String)


81
82
83
# File 'lib/queries/query.rb', line 81

def start_and_end_wildcard
  '%' + query_string + '%'
end

- (String) start_wildcard

Returns:

  • (String)


71
72
73
# File 'lib/queries/query.rb', line 71

def start_wildcard
  '%' + query_string
end

- (String) wildcard_pieces

Returns if `foo, and 123 and stuff` then %foo%and%123%and%stuff%

Returns:

  • (String)

    if `foo, and 123 and stuff` then %foo%and%123%and%stuff%



146
147
148
# File 'lib/queries/query.rb', line 146

def wildcard_pieces
  '%' + query_string.gsub(/[\s\W]+/, '%') + '%'
end

- (Array) wildcard_wrapped_integers

Returns:

  • (Array)


135
136
137
# File 'lib/queries/query.rb', line 135

def wildcard_wrapped_integers
  integers.collect{|i| "%#{i}%"}
end

- (Array) wildcard_wrapped_years

Returns:

  • (Array)


140
141
142
# File 'lib/queries/query.rb', line 140

def wildcard_wrapped_years
  years.collect{|i| "%#{i}%"}
end

- (Arel::Nodes::Matches) with_cached

Returns:

  • (Arel::Nodes::Matches)


266
267
268
# File 'lib/queries/query.rb', line 266

def with_cached
  table[:cached].eq(query_string)
end

- (Arel::Nodes::Matches) with_cached_like

Returns:

  • (Arel::Nodes::Matches)


271
272
273
# File 'lib/queries/query.rb', line 271

def with_cached_like
  table[:cached].matches(start_and_end_wildcard)
end

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



185
186
187
188
189
190
191
# File 'lib/queries/query.rb', line 185

def with_id
  if integers.any?
    table[:id].eq_any(integers)
  else
    nil
  end
end

- (Arel::Nodes::Equality) with_project_id

TODO: nil/or clause this

Returns:

  • (Arel::Nodes::Equality)


246
247
248
249
250
251
252
# File 'lib/queries/query.rb', line 246

def with_project_id
  if project_id
    table[:project_id].eq(project_id)
  else
    nil
  end
end

- (Object) with_start_date



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/queries/query.rb', line 212

def with_start_date
  if d = simple_date
    r = []
    r.push(table[:start_date_day].eq(d.day)) if d.day
    r.push(table[:start_date_month].eq(d.month)) if d.month
    r.push(table[:start_date_year].eq(d.year)) if d.year

    q = r.pop
    r.each do |z|
      q = q.and(z)
    end
    q
  else
    nil
  end
end

- (String?) year_letter

Returns the first letter recognized as coming directly past the first year

`Smith, 1920a. ... ` returns `a`

Returns:

  • (String, nil)

    the first letter recognized as coming directly past the first year

    `Smith, 1920a. ... ` returns `a`


99
100
101
# File 'lib/queries/query.rb', line 99

def year_letter
  query_string.match(/\d{4}([a-zAZ]+)/).to_a.last
end

- (Array) years

Returns those strings that represent years

Returns:

  • (Array)

    those strings that represent years



92
93
94
# File 'lib/queries/query.rb', line 92

def years
  query_string.scan(/\d{4}/).to_a.uniq
end