/
entry_search.rb
288 lines (247 loc) · 8.55 KB
/
entry_search.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
256
257
258
259
260
261
262
263
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
class EntrySearch < ApplicationSearch
class CFR < Struct.new(:title,:part)
TITLE_MULTIPLIER = 100000
def citation
if part
"#{title} CFR #{part}"
else
"#{title} CFR"
end
end
def sphinx_citation
title_int = title.to_s.to_i * TITLE_MULTIPLIER
if part.present?
title_int + part.to_s.to_i
else
title_int ... title_int + TITLE_MULTIPLIER
end
end
end
TYPES = [
['Rule', 'RULE' ],
['Proposed Rule', 'PRORULE' ],
['Notice', 'NOTICE' ],
['Presidential Document', 'PRESDOCU']
]
include Geokit::Geocoders
attr_reader :type
attr_accessor :type, :regulation_id_number, :prior_term
define_filter :regulation_id_number, :label => "Unified Agenda", :phrase => true do |regulation_id_number|
reg = RegulatoryPlan.find_by_regulation_id_number(regulation_id_number)
["RIN #{regulation_id_number}", reg.try(:title)].join(' - ')
end
def regulatory_plan_title
if @regulation_id_number.present?
RegulatoryPlan.find_by_regulation_id_number(@regulation_id_number, :order => "issue DESC").try(:title)
end
end
define_filter :agency_ids, :sphinx_type => :with
define_filter :section_ids, :sphinx_type => :with_all do |section_id|
Section.find_by_id(section_id).try(:title)
end
define_filter :topic_ids, :sphinx_type => :with_all
define_filter :type, :sphinx_type => :with, :crc32_encode => true do |types|
types.map{|type| Entry::ENTRY_TYPES[type]}.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ')
end
define_filter :presidential_document_type_id, :sphinx_type => :with
define_filter :small_entity_ids, :sphinx_type => :with, :label => "Small Entities Affected" do |entity_ids|
SmallEntity.find_all_by_id(entity_ids).map(&:name).to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ')
end
define_filter :docket_id, :phrase => true, :label => "Agency Docket" do |docket|
docket
end
define_filter :significant, :sphinx_type => :with, :label => "Significance" do
"Associated Unified Agenda Deemed Significant Under EO 12866"
end
define_place_filter :near, :sphinx_attribute => :place_ids
define_date_filter :publication_date, :label => "Publication Date"
define_date_filter :effective_date, :label => "Effective Date"
define_date_filter :comment_date, :label => "Comment Date"
attr_reader :cfr
def cfr=(hsh)
hsh = hsh.with_indifferent_access
if hsh.present? && hsh.values.any?(&:present?)
@cfr = CFR.new(hsh[:title], hsh[:part])
if @cfr.title.present?
add_filter(
:value => @cfr.sphinx_citation,
:name => @cfr.citation,
:condition => :cfr,
:sphinx_attribute => :cfr_affected_parts,
:label => "Affected CFR Part",
:sphinx_type => :with
)
else
@errors[:cfr] = "You must provide at least a CFR title"
end
end
end
def model
Entry
end
def find_options
{
:select => "id, title, publication_date, document_number, granule_class, document_file_path, abstract, length, start_page, end_page, citation",
:include => :agencies,
}
end
def supported_orders
%w(Relevant Newest Oldest)
end
def order_clause
case @order
when 'newest', 'date'
"publication_date DESC, @relevance DESC"
when 'oldest'
"publication_date ASC, @relevance DESC"
else
"@relevance DESC, publication_date DESC, start_page ASC, sphinx_internal_id ASC"
end
end
def agency_facets
ApplicationSearch::FacetCalculator.new(:search => self, :model => Agency, :facet_name => :agency_ids).all
end
memoize :agency_facets
def section_facets
ApplicationSearch::FacetCalculator.new(:search => self, :model => Section, :facet_name => :section_ids, :name_attribute => :title).all
end
memoize :section_facets
def topic_facets
ApplicationSearch::FacetCalculator.new(:search => self, :model => Topic, :facet_name => :topic_ids).all
end
memoize :topic_facets
def type_facets
ApplicationSearch::FacetCalculator.new(:search => self, :facet_name => :type, :hash => Entry::ENTRY_TYPES).all().reject do |facet|
["UNKNOWN", "CORRECT"].include?(facet.value)
end
end
memoize :type_facets
def date_distribution(options = {})
options[:since] ||= Date.parse('1994-01-01')
sphinx_search = ThinkingSphinx::Search.new(sphinx_term,
:with => with.merge(:publication_date => options[:since].to_time .. 1.week.from_now),
:with_all => with_all,
:conditions => sphinx_conditions,
:match_mode => :extended
)
klass = case options.delete(:period)
when :weekly
EntrySearch::DateAggregator::Weekly
when :monthly
EntrySearch::DateAggregator::Monthly
when :quarterly
EntrySearch::DateAggregator::Quarterly
else
raise "invalid :period specified; must be one of :weekly, :monthly, or :quarterly"
end
distribution = klass.new(sphinx_search, options)
distribution.results
end
def count_in_last_n_days(n)
model.search_count(sphinx_term,
:with => with.merge(:publication_date => n.days.ago.to_time.midnight .. Time.current.midnight),
:with_all => with_all,
:conditions => sphinx_conditions,
:match_mode => :extended
)
end
def publication_date_facets
facets = [30,90,365].map do |n|
value = n.days.ago.to_date.to_s
ApplicationSearch::Facet.new(
:value => {:gte => value},
:name => "Past #{n} days",
:count => count_in_last_n_days(n),
:condition => :publication_date
)
end
if facets.all?{|f| f.count == 0}
return []
else
facets
end
end
memoize :publication_date_facets
def regulatory_plan
if @regulation_id_number
RegulatoryPlan.find_by_regulation_id_number(@regulation_id_number)
end
end
memoize :regulatory_plan
def matching_entry_citation
if term.present?
term.scan(/^\s*(\d+)\s*(?:F\.?R\.?|Fed\.?\s*Reg\.?)\s*(\d+)\s*$/i) do |volume, page|
return Citation.new(:citation_type => "FR", :part_1 => volume.to_i, :part_2 => page.to_i)
end
term.scan(/^\s*(?:EO|Executive Order|E\.O\.)\s+(\d+)\s*$/i) do |captures|
return Citation.new(:citation_type => "EO", :part_1 => captures.first.to_i)
end
end
return nil
end
def suggestion
if !defined?(@suggestion)
@suggestion = [
EntrySearch::Suggestor::Agency,
EntrySearch::Suggestor::Cfr,
EntrySearch::Suggestor::Date,
EntrySearch::Suggestor::EntryType,
EntrySearch::Suggestor::RegulationIdNumber,
EntrySearch::Suggestor::Spelling
].reduce(self) {|suggestion, suggestor| suggestor.new(suggestion).suggestion || suggestion }
@suggestion = nil if @suggestion == self
end
@suggestion
end
def entry_with_document_number
if term.present?
return Entry.find_by_document_number(term)
end
end
def summary
if @term.blank? && filters.empty?
"All Articles"
else
parts = filter_summary
parts.unshift("matching '#{@term}'") if @term.present?
'Articles ' + parts.to_sentence
end
end
def filter_summary
parts = []
[
['published', :publication_date],
['with an effective date', :effective_date],
['from', :agency_ids],
['of type', :type],
['filed under agency docket', :docket_id],
['whose', :significant],
['associated with', :regulation_id_number],
['affecting', :cfr],
['located', :near],
['in', :section_ids],
['about', :topic_ids],
['affecting Small', :small_entity_ids]
].each do |term, filter_condition|
relevant_filters = filters.select{|f| f.condition == filter_condition}
unless relevant_filters.empty?
parts << "#{term} #{relevant_filters.map(&:name).to_sentence(:two_words_connector => ' or ', :last_word_connector => ', and ')}"
end
end
parts
end
def results_for_date(date, args = {})
date = ApplicationSearch::DateSelector.new(:is => date)
results({:with => {:publication_date => date.sphinx_value}, :per_page => 1000}.merge(args))
end
def public_inspection_search_possible?
PublicInspectionDocumentSearch.valid_arguments?(
:conditions => valid_conditions
)
end
private
def set_defaults(options)
@within = 25
@order = options[:order] || 'relevant'
end
end