Class: Queries::Query

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

Direct Known Subclasses

ControlledVocabularyTerm::Autocomplete, Descriptor::Autocomplete, Otu::Autocomplete, Otu::Filter, BiologicalCollectionObjectAutocompleteQuery, CollectionObjectFilterQuery, ContainerAutocompleteQuery, ControlledVocabularyTermAutocompleteQuery, 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)


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

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

Instance Attribute Details

- (Integer) dynamic_limit

Returns:

  • (Integer)


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

def dynamic_limit
  @dynamic_limit
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)


51
52
53
# File 'lib/queries/query.rb', line 51

def terms
  @terms
end

Instance Method Details

- (Array) alphabetic_strings

Returns:

  • (Array)


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

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



269
270
271
# File 'lib/queries/query.rb', line 269

def autocomplete
  all.to_a
end

- (ActiveRecord::Relation) autocomplete_cached

Returns:

  • (ActiveRecord::Relation)


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

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!



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

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)


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

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



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

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



235
236
237
238
239
240
241
# File 'lib/queries/query.rb', line 235

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)


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

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)


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

def end_wildcard
  query_string + '%'
end

- (Arel::Nodes::Matches) exactly_named

Returns:

  • (Arel::Nodes::Matches)


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

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.



111
112
113
114
115
116
117
118
# File 'lib/queries/query.rb', line 111

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)


217
218
219
# File 'lib/queries/query.rb', line 217

def identifier_table
  Identifier.arel_table
end

- (Array) integers

Returns of strings representing integers

Returns:

  • (Array)

    of strings representing integers



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

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)


252
253
254
# File 'lib/queries/query.rb', line 252

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)


245
246
247
248
249
# File 'lib/queries/query.rb', line 245

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)


192
193
194
# File 'lib/queries/query.rb', line 192

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

- (String) no_digits

Returns:

  • (String)


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

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.



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

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



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

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

- (Arel::Nodes::TableAlias) parent

Returns:

  • (Arel::Nodes::TableAlias)


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

def parent
  table.alias
end

- (Scope) parent_child_join

Returns:

  • (Scope)


159
160
161
# File 'lib/queries/query.rb', line 159

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)


165
166
167
168
169
# File 'lib/queries/query.rb', line 165

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



122
123
124
# File 'lib/queries/query.rb', line 122

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



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

def result
  []
end

- (Scope) scope

stub

Returns:

  • (Scope)


39
40
41
# File 'lib/queries/query.rb', line 39

def scope
  where('1 = 2')
end

- (String) start_and_end_wildcard

Returns:

  • (String)


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

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

- (String) start_wildcard

Returns:

  • (String)


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

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%



133
134
135
# File 'lib/queries/query.rb', line 133

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

- (Array) wildcard_wrapped_integers

Returns:

  • (Array)


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

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

- (Query?) with_id

Returns used in or_clauses

Returns:

  • (Query, nil)

    used in or_clauses



173
174
175
176
177
178
179
# File 'lib/queries/query.rb', line 173

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

- (Arel::Nodes::Equality) with_identifier

Returns:

  • (Arel::Nodes::Equality)


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

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

- (Arel::Nodes::Grouping) with_identifier_like

Returns:

  • (Arel::Nodes::Grouping)


222
223
224
225
226
# File 'lib/queries/query.rb', line 222

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)


208
209
210
211
212
213
214
# File 'lib/queries/query.rb', line 208

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`


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

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



84
85
86
# File 'lib/queries/query.rb', line 84

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