Skip to content

Commit

Permalink
Fix date-based facet handling to ensure values are properly escaped b…
Browse files Browse the repository at this point in the history
…efore sending to solr; fixes #866
  • Loading branch information
cbeer committed Apr 6, 2014
1 parent 3d1b5ea commit 960cbeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/blacklight/request_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,14 @@ def facet_value_to_fq_string(facet_field, value)
fq = case
when (facet_config and facet_config.query)
facet_config.query[value][:fq]
when (facet_config and facet_config.date),
(value.is_a?(TrueClass) or value.is_a?(FalseClass) or value == 'true' or value == 'false'),
when (facet_config and facet_config.date)
# in solr 3.2+, this could be replaced by a !term query
"#{prefix}#{facet_field}:#{escape(value)}"
when (value.is_a?(DateTime) or value.is_a?(Date) or value.is_a?(Time))
"#{prefix}#{facet_field}:#{escape(value.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ"))}"
when (value.is_a?(TrueClass) or value.is_a?(FalseClass) or value == 'true' or value == 'false'),
(value.is_a?(Integer) or (value.to_i.to_s == value if value.respond_to? :to_i)),
(value.is_a?(Float) or (value.to_f.to_s == value if value.respond_to? :to_f))
(value.is_a?(DateTime) or value.is_a?(Date) or value.is_a?(Time))
"#{prefix}#{facet_field}:#{value}"
when value.is_a?(Range)
"#{prefix}#{facet_field}:[#{value.first} TO #{value.last}]"
Expand All @@ -282,5 +285,9 @@ def facet_value_to_fq_string(facet_field, value)


end

def escape value
value.gsub(/(\W)/, '\\\\\1')
end
end
end
14 changes: 13 additions & 1 deletion spec/lib/blacklight/solr_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,19 @@ def logger
it "should pass date-type fields through" do
blacklight_config.facet_fields.stub(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))

expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012-01-01"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01"
end

it "should escape datetime-type fields" do
blacklight_config.facet_fields.stub(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))

expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z"
end

it "should format Date objects correctly" do
blacklight_config.facet_fields.stub(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil))
d = DateTime.parse("2003-04-09T00:00:00")
expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z"
end

it "should handle range requests" do
Expand Down

0 comments on commit 960cbeb

Please sign in to comment.