Class: Queries::Source::Autocomplete
- Inherits:
-
Query
- Object
- Query
- Queries::Source::Autocomplete
show all
- Defined in:
- lib/queries/source/autocomplete.rb
Instance Attribute Summary collapse
Attributes inherited from Query
#dynamic_limit, #options, #query_string, #terms
Instance Method Summary
collapse
Methods inherited from Query
#alphabetic_strings, #alphanumeric_strings, #attribute_exact_facet, #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, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #string_fragments, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years
Constructor Details
#initialize(string, project_id: nil, limit_to_project: false) ⇒ Autocomplete
Returns a new instance of Autocomplete.
14
15
16
17
|
# File 'lib/queries/source/autocomplete.rb', line 14
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
8
9
10
|
# File 'lib/queries/source/autocomplete.rb', line 8
def limit_to_project
@limit_to_project
end
|
#project_id ⇒ Object
Comes from sessions_current_project_id in UI
11
12
13
|
# File 'lib/queries/source/autocomplete.rb', line 11
def project_id
@project_id
end
|
Instance Method Details
#author_from_author_year ⇒ String
184
185
186
187
|
# File 'lib/queries/source/autocomplete.rb', line 184
def author_from_author_year
query_string.match(/[[[:word:]]]+/).to_a.last
end
|
#autocomplete ⇒ Array
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/queries/source/autocomplete.rb', line 207
def autocomplete
queries = [
[ autocomplete_exact_id, false],
[ autocomplete_identifier_identifier_exact, false],
[ autocomplete_exact_author_year_letter&.limit(20), true],
[ autocomplete_identifier_cached_exact, false],
[ autocomplete_exact_author_year&.limit(20), true],
[ autocomplete_start_author_year&.limit(20), true],
[ autocomplete_wildcard_author_exact_year&.limit(20), true],
[ autocomplete_exact_author&.limit(20), true],
[ autocomplete_start_of_author&.limit(20), 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?}
result = []
queries.each do |q, scope|
a = q
if project_id && limit_to_project
a = a.joins(:project_sources).where(member_of_project_id.to_sql)
end
if project_id && scope
a = a.left_outer_joins(:citations)
.select('sources.*, COUNT(citations.id) AS use_count, NULLIF(citations.project_id, NULL) as in_project')
.where('citations.project_id = ? OR citations.project_id IS DISTINCT FROM ?', project_id, project_id)
.group('sources.id, citations.project_id')
.order('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.
34
35
36
37
|
# File 'lib/queries/source/autocomplete.rb', line 34
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.
26
27
28
29
|
# File 'lib/queries/source/autocomplete.rb', line 26
def autocomplete_exact_author
a = table[:cached_author_string].matches(query_string)
base_query.where(a.to_sql)
end
|
#autocomplete_exact_author_year ⇒ ActiveRecord::Relation?
105
106
107
108
109
110
111
112
|
# File 'lib/queries/source/autocomplete.rb', line 105
def autocomplete_exact_author_year
return nil if query_string.split(' ').count > 2
a = match_exact_author
d = match_year
return nil if a.nil? || d.nil?
z = a.and(d)
base_query.where(z.to_sql)
end
|
#autocomplete_exact_author_year_letter ⇒ ActiveRecord::Relation?
95
96
97
98
99
100
101
102
|
# File 'lib/queries/source/autocomplete.rb', line 95
def autocomplete_exact_author_year_letter
a = match_exact_author
d = match_year_suffix
c = match_year
return nil if [a,d,c].include?(nil)
z = a.and(d).and(c)
base_query.where(z.to_sql)
end
|
#autocomplete_exact_in_cached ⇒ ActiveRecord::Relation?
135
136
137
138
139
|
# File 'lib/queries/source/autocomplete.rb', line 135
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.
49
50
51
52
|
# File 'lib/queries/source/autocomplete.rb', line 49
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?
115
116
117
118
119
120
121
122
|
# File 'lib/queries/source/autocomplete.rb', line 115
def autocomplete_start_author_year
return nil if query_string.split(' ').count > 2
a = match_start_author
d = match_year
return nil if a.nil? || d.nil?
z = a.and(d)
base_query.where(z.to_sql)
end
|
#autocomplete_start_of_author ⇒ ActiveRecord::Relation
Returns author matches start.
41
42
43
44
|
# File 'lib/queries/source/autocomplete.rb', line 41
def autocomplete_start_of_author
a = table[:cached_author_string].matches(query_string + '%')
base_query.where(a.to_sql)
end
|
#autocomplete_start_of_title ⇒ ActiveRecord::Relation
Returns title matches start.
63
64
65
66
|
# File 'lib/queries/source/autocomplete.rb', line 63
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?
142
143
144
145
146
147
148
|
# File 'lib/queries/source/autocomplete.rb', line 142
def autocomplete_wildcard_anywhere_exact_year
a = match_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_author_exact_year ⇒ ActiveRecord::Relation?
125
126
127
128
129
130
131
132
|
# File 'lib/queries/source/autocomplete.rb', line 125
def autocomplete_wildcard_author_exact_year
return nil if query_string.split(' ').count > 2
a = match_year
d = match_wildcard_author
return nil if a.nil? || d.nil?
z = a.and(d)
base_query.where(z.to_sql)
end
|
#autocomplete_wildcard_of_title_alternate ⇒ ActiveRecord::Relation
Returns title matches wildcard on alternate.
70
71
72
73
|
# File 'lib/queries/source/autocomplete.rb', line 70
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.
77
78
79
80
81
82
83
|
# File 'lib/queries/source/autocomplete.rb', line 77
def autocomplete_wildcard_pieces_and_year
a = match_ordered_wildcard_pieces_in_cached
b = match_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.
56
57
58
59
|
# File 'lib/queries/source/autocomplete.rb', line 56
def autocomplete_year
a = table[:year].eq_any(years)
base_query.where(a.to_sql).limit(5)
end
|
#autocomplete_year_letter ⇒ ActiveRecord::Relation?
86
87
88
89
90
91
92
|
# File 'lib/queries/source/autocomplete.rb', line 86
def autocomplete_year_letter
a = match_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
|
#base_query ⇒ Scope
20
21
22
|
# File 'lib/queries/source/autocomplete.rb', line 20
def base_query
::Source end
|
#fragment_year_matches ⇒ ActiveRecord::Relation?
Returns if user provides 5 or fewer strings and any number of years look for any string && year.
196
197
198
199
200
201
202
203
204
|
# File 'lib/queries/source/autocomplete.rb', line 196
def fragment_year_matches
if fragments.any?
s = table[:cached].matches_any(fragments)
s = s.and(table[:year].eq_any(years)) if !years.empty?
s
else
nil
end
end
|
#match_exact_author ⇒ Arel::Nodes::Equatity
159
160
161
|
# File 'lib/queries/source/autocomplete.rb', line 159
def match_exact_author
table[:cached_author_string].matches(author_from_author_year)
end
|
#match_start_author ⇒ Arel::Nodes::Equatity
164
165
166
167
168
|
# File 'lib/queries/source/autocomplete.rb', line 164
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
152
153
154
155
156
|
# File 'lib/queries/source/autocomplete.rb', line 152
def match_wildcard_author
b = fragments
return nil if b.empty?
a = table[:cached_author_string].matches_all(b)
end
|
#match_year ⇒ Arel::Nodes::Equatity
177
178
179
180
181
|
# File 'lib/queries/source/autocomplete.rb', line 177
def match_year
a = years.first
return nil if a.nil?
table[:year].eq(a)
end
|
#match_year_suffix ⇒ Arel::Nodes::Equatity
171
172
173
174
|
# File 'lib/queries/source/autocomplete.rb', line 171
def match_year_suffix
return nil if year_letter.blank?
table[:year_suffix].eq(year_letter)
end
|
#member_of_project_id ⇒ Arel::Nodes::Equatity
190
191
192
|
# File 'lib/queries/source/autocomplete.rb', line 190
def member_of_project_id
project_sources_table[:project_id].eq(project_id)
end
|
#project_sources_table ⇒ Arel::Table
263
264
265
|
# File 'lib/queries/source/autocomplete.rb', line 263
def project_sources_table
::ProjectSource.arel_table
end
|
#table ⇒ Arel::Table
258
259
260
|
# File 'lib/queries/source/autocomplete.rb', line 258
def table
::Source.arel_table
end
|