Class: Queries::Query

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

Direct Known Subclasses

BiologicalRelationship::Autocomplete, ControlledVocabularyTerm::Autocomplete, Descriptor::Autocomplete, Otu::Autocomplete, Otu::Filter, BiologicalCollectionObjectAutocompleteQuery, CollectionObjectFilterQuery, ContainerAutocompleteQuery, DocumentAutocompleteQuery, GeographicAreaAutocompleteQuery, NamespaceAutocompleteQuery, Person::Autocomplete, RepositoryAutocompleteQuery, Source::Autocomplete, Source::Filter, TagAutocompleteQuery, TaxonName::Autocomplete, TaxonNameRelationshipsFilterQuery, TypeMaterialAutocompleteQuery

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)


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

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

Instance Attribute Details

- (Integer) dynamic_limit

Returns:

  • (Integer)


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

def dynamic_limit
  @dynamic_limit
end

- (Object) options

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



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

def options
  @options
end

- (Object) project_id

Returns the value of attribute project_id



19
20
21
# File 'lib/queries/query.rb', line 19

def project_id
  @project_id
end

- (Object) query_string

Returns the value of attribute query_string



17
18
19
# File 'lib/queries/query.rb', line 17

def query_string
  @query_string
end

- (Array) terms

Returns:

  • (Array)


55
56
57
# File 'lib/queries/query.rb', line 55

def terms
  @terms
end

Instance Method Details

- (Array) alphabetic_strings

Returns:

  • (Array)


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

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



273
274
275
# File 'lib/queries/query.rb', line 273

def autocomplete
  all.to_a
end

- (ActiveRecord::Relation) autocomplete_cached

Returns:

  • (ActiveRecord::Relation)


293
294
295
296
297
# File 'lib/queries/query.rb', line 293

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!



286
287
288
289
290
# File 'lib/queries/query.rb', line 286

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

- (ActiveRecord::Relation) autocomplete_ordered_wildcard_pieces_in_cached

Returns:

  • (ActiveRecord::Relation)


279
280
281
# File 'lib/queries/query.rb', line 279

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

- (Array) build_terms

Ultimately we should replace this concept with full text indexing.

Returns:

  • (Array)

    a reasonable (starting) interpretation of any query string



62
63
64
# File 'lib/queries/query.rb', line 62

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

- (ActiveRecord::Relation?) cached

Returns cached matches full query string wildcarded

Returns:

  • (ActiveRecord::Relation, nil)

    cached matches full query string wildcarded



239
240
241
242
243
244
245
# File 'lib/queries/query.rb', line 239

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)


261
262
263
264
265
266
267
268
269
# File 'lib/queries/query.rb', line 261

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)


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

def end_wildcard
  query_string + '%'
end

- (Arel::Nodes::Matches) exactly_named

Returns:

  • (Arel::Nodes::Matches)


201
202
203
# File 'lib/queries/query.rb', line 201

def exactly_named
  table[:name].eq(query_string)
end

- (Array) fragments

Used in unordered AND searchs

Returns:

  • (Array)

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



115
116
117
118
119
120
121
122
# File 'lib/queries/query.rb', line 115

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

- (Arel::Table) identifier_table

Returns:

  • (Arel::Table)


221
222
223
# File 'lib/queries/query.rb', line 221

def identifier_table
  Identifier.arel_table
end

- (Array) integers

Returns of strings representing integers

Returns:

  • (Array)

    of strings representing integers



101
102
103
104
# File 'lib/queries/query.rb', line 101

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)


256
257
258
# File 'lib/queries/query.rb', line 256

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

- (Arel::Nodes::Matches) match_wildcard_cached

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

Returns:

  • (Arel::Nodes::Matches)


249
250
251
252
253
# File 'lib/queries/query.rb', line 249

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

- (Arel::Nodes::Matches) named

Returns:

  • (Arel::Nodes::Matches)


196
197
198
# File 'lib/queries/query.rb', line 196

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

- (String) no_digits

Returns:

  • (String)


142
143
144
# File 'lib/queries/query.rb', line 142

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.



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

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



108
109
110
# File 'lib/queries/query.rb', line 108

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

- (Arel::Nodes::TableAlias) parent

Returns:

  • (Arel::Nodes::TableAlias)


206
207
208
# File 'lib/queries/query.rb', line 206

def parent
  table.alias
end

- (Scope) parent_child_join

Returns:

  • (Scope)


163
164
165
# File 'lib/queries/query.rb', line 163

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)


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

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



126
127
128
# File 'lib/queries/query.rb', line 126

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



37
38
39
# File 'lib/queries/query.rb', line 37

def result
  []
end

- (Scope) scope

stub

Returns:

  • (Scope)


43
44
45
# File 'lib/queries/query.rb', line 43

def scope
  where('1 = 2')
end

- (String) start_and_end_wildcard

Returns:

  • (String)


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

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

- (String) start_wildcard

Returns:

  • (String)


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

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%



137
138
139
# File 'lib/queries/query.rb', line 137

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

- (Array) wildcard_wrapped_integers

Returns:

  • (Array)


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

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

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



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

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

- (Arel::Nodes::Equality) with_identifier

Returns:

  • (Arel::Nodes::Equality)


233
234
235
# File 'lib/queries/query.rb', line 233

def with_identifier
  identifier_table[:cached].eq(query_string)
end

- (Arel::Nodes::Grouping) with_identifier_like

Returns:

  • (Arel::Nodes::Grouping)


226
227
228
229
230
# File 'lib/queries/query.rb', line 226

def with_identifier_like
  a = [ start_and_end_wildcard ]
  a = a + wildcard_wrapped_integers
  identifier_table[:cached].matches_any(a)
end

- (Arel::Nodes::Equality) with_project_id

TODO: nil/or clause this

Returns:

  • (Arel::Nodes::Equality)


212
213
214
215
216
217
218
# File 'lib/queries/query.rb', line 212

def with_project_id
  if project_id
    table[:project_id].eq(project_id)
  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`


95
96
97
# File 'lib/queries/query.rb', line 95

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



88
89
90
# File 'lib/queries/query.rb', line 88

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