Skip to content

Commit

Permalink
Merge pull request #3213 from taylor-steve/facet-to-fq-nil-range
Browse files Browse the repository at this point in the history
Handle beginless/endless ranges in facet value
  • Loading branch information
jrochkind authored Nov 13, 2024
2 parents af7af82 + d460da6 commit 1494bd0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/blacklight/solr/search_builder_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def solr_param_quote(val, options = {})

##
# Convert a facet/value pair into a solr fq parameter
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def facet_value_to_fq_string(facet_field, value, use_local_params: true)
facet_config = blacklight_config.facet_fields[facet_field]

Expand All @@ -361,14 +361,16 @@ def facet_value_to_fq_string(facet_field, value, use_local_params: true)
end
elsif value.is_a?(Range)
prefix = "{!#{local_params.join(' ')}}" unless local_params.empty?
"#{prefix}#{solr_field}:[#{value.first} TO #{value.last}]"
start = value.begin || '*'
finish = value.end || '*'
"#{prefix}#{solr_field}:[#{start} TO #{finish}]"
elsif value == Blacklight::SearchState::FilterField::MISSING
"-#{solr_field}:[* TO *]"
else
"{!term f=#{solr_field}#{" #{local_params.join(' ')}" unless local_params.empty?}}#{convert_to_term_value(value)}"
end
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def facet_inclusive_value_to_fq_string(facet_field, values)
return if values.blank?
Expand Down
3 changes: 3 additions & 0 deletions spec/models/blacklight/solr/search_builder_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@

it "handles range requests" do
expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..5)).to eq "facet_name:[1 TO 5]"
expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..nil)).to eq "facet_name:[1 TO *]"
expect(subject.send(:facet_value_to_fq_string, "facet_name", nil..5)).to eq "facet_name:[* TO 5]"
expect(subject.send(:facet_value_to_fq_string, "facet_name", nil..nil)).to eq "facet_name:[* TO *]"
end

it "adds tag local parameters" do
Expand Down

0 comments on commit 1494bd0

Please sign in to comment.