From 0bac3e35b5118a938356dd6fd5118c44f46ec086 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Wed, 18 Mar 2015 19:10:19 -0700 Subject: [PATCH] Mapping using facet_value_to_fq_string should respect blacklight's facet configuration --- lib/blacklight/solr/search_builder.rb | 13 ++++++++----- spec/lib/blacklight/solr/search_builder_spec.rb | 12 ++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/blacklight/solr/search_builder.rb b/lib/blacklight/solr/search_builder.rb index edc22ef0b4..557a5035b4 100644 --- a/lib/blacklight/solr/search_builder.rb +++ b/lib/blacklight/solr/search_builder.rb @@ -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 @@ -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 diff --git a/spec/lib/blacklight/solr/search_builder_spec.rb b/spec/lib/blacklight/solr/search_builder_spec.rb index 8b6467a67a..ae695fad5e 100644 --- a/spec/lib/blacklight/solr/search_builder_spec.rb +++ b/spec/lib/blacklight/solr/search_builder_spec.rb @@ -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" @@ -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 @@ -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"