Class: Queries::Source::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/source/autocomplete.rb

Constant Summary collapse

SOURCE_ROLE_TYPES =
['SourceAuthor', 'SourceEditor', 'SourceSource'].freeze

Constants inherited from Query::Autocomplete

Query::Autocomplete::DEFAULT_LIMIT, 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 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, limit_to_project: false) ⇒ Autocomplete

Returns a new instance of Autocomplete.



12
13
14
15
# File 'lib/queries/source/autocomplete.rb', line 12

def initialize(string, project_id: nil, limit_to_project: false)
  @limit_to_project = limit_to_project
  super
end

Instance Attribute Details

#limit_to_project ⇒ Boolean

Either match against all Sources (default) or just those with ProjectSource

Parameters:

  • limit_to_project (String) —

    true or false

Returns:

  • (Boolean)


10
11
12
# File 'lib/queries/source/autocomplete.rb', line 10

def limit_to_project
  @limit_to_project
end

Instance Method Details

#author_from_author_year ⇒ String

Returns:

  • (String)


241
242
243
244
# File 'lib/queries/source/autocomplete.rb', line 241

def author_from_author_year
  query_string.match(/[[[:word:]]]+/).to_a.last
  #query_string.match(/^(.+?)\W/).to_a.last
end

#autocomplete ⇒ Array

Returns:

  • (Array)


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/queries/source/autocomplete.rb', line 264

def autocomplete

  # [ query, order by use if true- don't if nil ]
  queries = [
    [ autocomplete_exact_id, false],
    [ autocomplete_identifier_identifier_exact, false],
    [ autocomplete_exact_author_year_letter&.limit(20), true],
    [ autocomplete_exact_author_year_letter_alternate&.limit(20), true],
    [ autocomplete_identifier_cached_exact, false],
    [ autocomplete_exact_author_year&.limit(20), true],
    [ autocomplete_exact_author_year_alternate&.limit(20), true],
    [ autocomplete_start_author_year&.limit(20), true],
    [ autocomplete_start_author_year_alternate&.limit(20), true],
    [ autocomplete_exact_author&.limit(20), true],
    [ autocomplete_exact_author_alternate&.limit(20), true],
    [ autocomplete_start_of_author&.limit(20), true],
    [ autocomplete_start_of_author_alternate&.limit(20), true],
    #[ autocomplete_wildcard_anywhere_exact_year&.limit(10), true],
    [ autocomplete_identifier_cached_like, true],
    [ autocomplete_exact_in_cached&.limit(20), true],
    [ autocomplete_ordered_wildcard_pieces_in_cached&.limit(20), true],
    [ autocomplete_cached_wildcard_anywhere&.limit(20), true],
    [ autocomplete_start_of_title&.limit(20), true],
    [ autocomplete_wildcard_of_title_alternate&.limit(20), true]
  ]

  queries.delete_if{|a,b| a.nil?} # Note this pattern differs because [[]] so we don't use compact. /lib/queries/repository/autocomplete.rb follows same pattern

  result = []

  pr_id = project_id.join(',') if project_id
  queries.each do |q, scope|
    a = q

    # Limit autocomplete to ONLY project sources if limit_to_project == true
    if project_id.present? && limit_to_project
      a = a.joins(:project_sources).where(member_of_project_id.to_sql)
    end

    # Order results by number of times used *in this project*
    if project_id.present? && scope && query_string.length > 3
      a = a.select(
          "sources.*, " \
          "(SELECT COUNT(*) FROM citations WHERE citations.source_id = sources.id) AS use_count, " \
          "CASE WHEN EXISTS (" \
            "SELECT 1 FROM project_sources WHERE project_sources.source_id = sources.id AND project_sources.project_id IN (#{pr_id})" \
          ") THEN 1 ELSE NULL END AS in_project"
        )
        .order('in_project, use_count DESC')
    end
    a ||= q
    result += a.to_a
    result.uniq!
    break if result.count > 19
  end
  result[0..19]
end

#autocomplete_any_author ⇒ ActiveRecord::Relation

Returns author matches any full word exactly !! Not used currently.

Returns:

  • (ActiveRecord::Relation) —

    author matches any full word exactly !! Not used currently



37
38
39
40
# File 'lib/queries/source/autocomplete.rb', line 37

def autocomplete_any_author
  a = table[:cached_author_string].matches_regexp('\m' + query_string + '\M')
  base_query.where(a.to_sql).limit(20)
end

#autocomplete_exact_author ⇒ ActiveRecord::Relation

Returns if and only iff author string matches.

Returns:

  • (ActiveRecord::Relation) —

    if and only iff author string matches



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

def autocomplete_exact_author
  a = table[:cached_author_string].matches(query_string)
  base_query.where(a.to_sql)
end

#autocomplete_exact_author_alternate ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


25
26
27
28
29
30
31
32
# File 'lib/queries/source/autocomplete.rb', line 25

def autocomplete_exact_author_alternate
  ::Source
    .joins(roles: {person: :alternate_values})
    .where(roles: {type: SOURCE_ROLE_TYPES})
    .where(alternate_values: {alternate_value_object_attribute: 'last_name'})
    .where(alternate_values: {value: query_string})
    .limit(20)
end

#autocomplete_exact_author_year ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


136
137
138
139
140
141
142
143
# File 'lib/queries/source/autocomplete.rb', line 136

def autocomplete_exact_author_year
  return nil if query_string.split(' ').count > 2
  a = match_exact_author
  d = match_year_or_stated_year
  return nil if a.nil? || d.nil?
  z = a.and(d)
  base_query.where(z.to_sql)
end

#autocomplete_exact_author_year_alternate ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/queries/source/autocomplete.rb', line 146

def autocomplete_exact_author_year_alternate
  return nil if query_string.split(' ').count > 2
  author = author_from_author_year
  d = match_year_or_stated_year
  return nil if author.nil? || d.nil?

  ::Source
    .joins(roles: {person: :alternate_values})
    .where(roles: {type: SOURCE_ROLE_TYPES})
    .where(d.to_sql)
    .where(alternate_values: {alternate_value_object_attribute: 'last_name'})
    .where('alternate_values.value = ?', author)
    .limit(20)
end

#autocomplete_exact_author_year_letter ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


109
110
111
112
113
114
115
116
# File 'lib/queries/source/autocomplete.rb', line 109

def autocomplete_exact_author_year_letter
  a = match_exact_author
  d = match_year_suffix
  c = match_year_or_stated_year
  return nil if [a,d,c].include?(nil)
  z = a.and(d).and(c)
  base_query.where(z.to_sql)
end

#autocomplete_exact_author_year_letter_alternate ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/queries/source/autocomplete.rb', line 119

def autocomplete_exact_author_year_letter_alternate
  author = author_from_author_year
  d = match_year_suffix
  c = match_year_or_stated_year
  return nil if [author,d,c].include?(nil)
  z = d.and(c)

  ::Source
    .joins(roles: {person: :alternate_values})
    .where(roles: {type: SOURCE_ROLE_TYPES})
    .where(z.to_sql)
    .where(alternate_values: {alternate_value_object_attribute: 'last_name'})
    .where('alternate_values.value = ?', author)
    .limit(20)
end

#autocomplete_exact_in_cached ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


188
189
190
191
192
# File 'lib/queries/source/autocomplete.rb', line 188

def autocomplete_exact_in_cached
  a = with_cached_like
  return nil if a.nil?
  base_query.where(a.to_sql)
end

#autocomplete_partial_author ⇒ ActiveRecord::Relation

Returns author matches partial string !! Not used currently.

Returns:

  • (ActiveRecord::Relation) —

    author matches partial string !! Not used currently



62
63
64
65
# File 'lib/queries/source/autocomplete.rb', line 62

def autocomplete_partial_author
  a = table[:cached_author_string].matches('%' + query_string + '%')
  base_query.where(a.to_sql).limit(5)
end

#autocomplete_start_author_year ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


162
163
164
165
166
167
168
169
# File 'lib/queries/source/autocomplete.rb', line 162

def autocomplete_start_author_year
  return nil if query_string.split(' ').count > 2
  a = match_start_author
  d = match_year_or_stated_year
  return nil if a.nil? || d.nil?
  z = a.and(d)
  base_query.where(z.to_sql)
end

#autocomplete_start_author_year_alternate ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/queries/source/autocomplete.rb', line 172

def autocomplete_start_author_year_alternate
  return nil if query_string.split(' ').count > 2
  author = author_from_author_year
  d = match_year_or_stated_year
  return nil if author.nil? || d.nil?

  ::Source
    .joins(roles: {person: :alternate_values})
    .where(roles: {type: SOURCE_ROLE_TYPES})
    .where(d.to_sql)
    .where(alternate_values: {alternate_value_object_attribute: 'last_name'})
    .where('alternate_values.value ILIKE ?', "#{author}%")
    .limit(20)
end

#autocomplete_start_of_author ⇒ ActiveRecord::Relation

Returns author matches start.

Returns:

  • (ActiveRecord::Relation) —

    author matches start



44
45
46
47
# File 'lib/queries/source/autocomplete.rb', line 44

def autocomplete_start_of_author
  a = table[:cached_author_string].matches(query_string + '%')
  base_query.where(a.to_sql)
end

#autocomplete_start_of_author_alternate ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


50
51
52
53
54
55
56
57
# File 'lib/queries/source/autocomplete.rb', line 50

def autocomplete_start_of_author_alternate
  ::Source
    .joins(roles: {person: :alternate_values})
    .where(roles: {type: SOURCE_ROLE_TYPES})
    .where(alternate_values: {alternate_value_object_attribute: 'last_name'})
    .where('alternate_values.value ILIKE ?', "#{query_string}%")
    .limit(20)
end

#autocomplete_start_of_title ⇒ ActiveRecord::Relation

Returns title matches start.

Returns:

  • (ActiveRecord::Relation) —

    title matches start



76
77
78
79
# File 'lib/queries/source/autocomplete.rb', line 76

def autocomplete_start_of_title
  a = table[:title].matches(query_string + '%')
  base_query.where(a.to_sql)
end

#autocomplete_wildcard_anywhere_exact_year ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


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

def autocomplete_wildcard_anywhere_exact_year
  a = match_year_or_stated_year
  b = match_wildcard_in_cached
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql)
end

#autocomplete_wildcard_of_title_alternate ⇒ ActiveRecord::Relation

Returns title matches wildcard on alternate.

Returns:

  • (ActiveRecord::Relation) —

    title matches wildcard on alternate



83
84
85
86
# File 'lib/queries/source/autocomplete.rb', line 83

def autocomplete_wildcard_of_title_alternate
  base_query.joins(:alternate_values).
    where("alternate_values.alternate_value_object_attribute = 'title' AND alternate_values.value ILIKE ?", '%' + query_string + '%')
end

#autocomplete_wildcard_pieces_and_year ⇒ ActiveRecord::Relation

Returns author matches partial string !! Not used currently.

Returns:

  • (ActiveRecord::Relation) —

    author matches partial string !! Not used currently



91
92
93
94
95
96
97
# File 'lib/queries/source/autocomplete.rb', line 91

def autocomplete_wildcard_pieces_and_year
  a = match_ordered_wildcard_pieces_in_cached
  b = match_year_or_stated_year
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(5)
end

#autocomplete_year ⇒ ActiveRecord::Relation

Returns multi-year match? otherwise pointless.

Returns:

  • (ActiveRecord::Relation) —

    multi-year match? otherwise pointless



69
70
71
72
# File 'lib/queries/source/autocomplete.rb', line 69

def autocomplete_year
  a = table[:year].in(years)
  base_query.where(a.to_sql).limit(5)
end

#autocomplete_year_letter ⇒ ActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


100
101
102
103
104
105
106
# File 'lib/queries/source/autocomplete.rb', line 100

def autocomplete_year_letter
  a = match_year_or_stated_year
  b = match_year_suffix
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(10)
end

#fragment_year_matches ⇒ ActiveRecord::Relation?

Returns if user provides 5 or fewer strings and any number of years look for any string && year.

Returns:

  • (ActiveRecord::Relation, nil) —

    if user provides 5 or fewer strings and any number of years look for any string && year



253
254
255
256
257
258
259
260
261
# File 'lib/queries/source/autocomplete.rb', line 253

def fragment_year_matches
  if fragments.any?
    s = table[:cached].matches_any(fragments)
    s = s.and(table[:year].in(years)) if !years.empty?
    s
  else
    nil
  end
end

#match_exact_author ⇒ Arel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


212
213
214
# File 'lib/queries/source/autocomplete.rb', line 212

def match_exact_author
  table[:cached_author_string].matches(author_from_author_year)
end

#match_start_author ⇒ Arel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


217
218
219
220
221
# File 'lib/queries/source/autocomplete.rb', line 217

def match_start_author
  a = author_from_author_year
  return nil if a.blank?
  table[:cached_author_string].matches(a + '%')
end

#match_wildcard_author ⇒ Arel::Nodes::Matches

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

Returns:

  • (Arel::Nodes::Matches)


205
206
207
208
209
# File 'lib/queries/source/autocomplete.rb', line 205

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

#match_year_or_stated_year ⇒ Arel::Nodes::Grouping?

Returns:

  • (Arel::Nodes::Grouping, nil)


230
231
232
233
234
235
236
237
238
# File 'lib/queries/source/autocomplete.rb', line 230

def match_year_or_stated_year
  a = years.first
  return nil if a.nil?

  year_match = table[:year].eq(a)
  stated_year_match = table[:stated_year].eq(a.to_s)

  year_match.or(stated_year_match)
end

#match_year_suffix ⇒ Arel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


224
225
226
227
# File 'lib/queries/source/autocomplete.rb', line 224

def match_year_suffix
  return nil if year_letter.blank?
  table[:year_suffix].eq(year_letter)
end

#member_of_project_id ⇒ Arel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


247
248
249
# File 'lib/queries/source/autocomplete.rb', line 247

def member_of_project_id
  project_sources_table[:project_id].in(project_id)
end

#project_sources_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


323
324
325
# File 'lib/queries/source/autocomplete.rb', line 323

def project_sources_table
  ::ProjectSource.arel_table
end