Skip to content

Commit

Permalink
Mapping using facet_value_to_fq_string should respect blacklight's fa…
Browse files Browse the repository at this point in the history
…cet configuration
  • Loading branch information
cbeer committed Mar 19, 2015
1 parent 4d3d970 commit 0bac3e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/blacklight/solr/search_builder.rb
Expand Up @@ -232,6 +232,9 @@ def solr_param_quote(val, options = {})
def facet_value_to_fq_string(facet_field, value)
facet_config = blacklight_config.facet_fields[facet_field]

solr_field = facet_config.field if facet_config and not facet_config.query
solr_field ||= facet_field

local_params = []
local_params << "tag=#{facet_config.tag}" if facet_config and facet_config.tag

Expand All @@ -243,17 +246,17 @@ def facet_value_to_fq_string(facet_field, value)
facet_config.query[value][:fq]
when (facet_config and facet_config.date)
# in solr 3.2+, this could be replaced by a !term query
"#{prefix}#{facet_field}:#{RSolr.solr_escape(value)}"
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value)}"
when (value.is_a?(DateTime) or value.is_a?(Date) or value.is_a?(Time))
"#{prefix}#{facet_field}:#{RSolr.solr_escape(value.to_time.utc.strftime("%Y-%m-%dT%H:%M:%SZ"))}"
"#{prefix}#{solr_field}:#{RSolr.solr_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))
"#{prefix}#{facet_field}:#{RSolr.solr_escape(value.to_s)}"
"#{prefix}#{solr_field}:#{RSolr.solr_escape(value.to_s)}"
when value.is_a?(Range)
"#{prefix}#{facet_field}:[#{value.first} TO #{value.last}]"
"#{prefix}#{solr_field}:[#{value.first} TO #{value.last}]"
else
"{!raw f=#{facet_field}#{(" " + local_params.join(" ")) unless local_params.empty?}}#{value}"
"{!raw f=#{solr_field}#{(" " + local_params.join(" ")) unless local_params.empty?}}#{value}"
end
end

Expand Down
12 changes: 8 additions & 4 deletions spec/lib/blacklight/solr/search_builder_spec.rb
Expand Up @@ -355,6 +355,10 @@


describe "#facet_value_to_fq_string" do
it "should use the configured field name" do
blacklight_config.add_facet_field :facet_key, field: "facet_name"
expect(subject.send(:facet_value_to_fq_string, "facet_key", "my value")).to eq "{!raw f=facet_name}my value"
end

it "should use the raw handler for strings" do
expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name}my value"
Expand Down Expand Up @@ -389,19 +393,19 @@
end

it "should pass date-type fields through" do
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil, :field => 'facet_name'))

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
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil, :field => 'facet_name'))

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
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil))
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil, :field => 'facet_name'))
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
Expand All @@ -411,7 +415,7 @@
end

it "should add tag local parameters" do
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil))
allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil, :field => 'facet_name'))

expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!tag=asdf}facet_name:true"
expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name tag=asdf}my value"
Expand Down

0 comments on commit 0bac3e3

Please sign in to comment.