Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate negative facets into fq to exclude docs with a given facet value #3093

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/blacklight/search_state/filter_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SearchState
# Modeling access to filter query parameters
class FilterField
MISSING = { missing: true }.freeze
EXCLUDE = { exclude: true }.freeze

# @!attribute config
# @return [Blacklight::Configuration::FacetField]
Expand Down Expand Up @@ -53,6 +54,11 @@ def add(item)
value = Blacklight::Engine.config.blacklight.facet_missing_param
end

if value.is_a?(Array) && value.include?(Blacklight::SearchState::FilterField::EXCLUDE)
url_key = "-#{key}"
value = value.without(Blacklight::SearchState::FilterField::EXCLUDE).first
end

param = inclusive_filters_key if value.is_a?(Array)

# value could be a string
Expand Down Expand Up @@ -88,6 +94,11 @@ def remove(item)
value = Blacklight::Engine.config.blacklight.facet_missing_param
end

if value.is_a?(Array) && value.include?(Blacklight::SearchState::FilterField::EXCLUDE)
url_key = "-#{key}"
value = value.without(Blacklight::SearchState::FilterField::EXCLUDE).first
end

param = inclusive_filters_key if value.is_a?(Array)

# need to dup the facet values too,
Expand All @@ -114,8 +125,14 @@ def values(except: [])
f_inclusive = [params.dig(:f_inclusive, key)] unless params.dig(inclusive_filters_key, key).blank? || except.include?(:inclusive_filters)
f_missing = [Blacklight::SearchState::FilterField::MISSING] if params.dig(filters_key, "-#{key}")&.any? { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param }
f_missing = [] if except.include?(:missing)

f + (f_inclusive || []) + (f_missing || [])
exclude_facets = Array(params.dig(filters_key, "-#{key}")).reject { |v| v == Blacklight::Engine.config.blacklight.facet_missing_param }
f_exclude = if except.include?(:filters) || exclude_facets.none?
[]
else
[exclude_facets.flatten.compact + [Blacklight::SearchState::FilterField::EXCLUDE]]
end

f + (f_inclusive || []) + (f_missing || []) + (f_exclude || [])
end
delegate :any?, to: :values

Expand All @@ -134,7 +151,11 @@ def include?(item)

case value
when Array
(params.dig(inclusive_filters_key, key) || []).to_set == value.to_set
if value.include?(Blacklight::SearchState::FilterField::EXCLUDE)
(params.dig(filters_key, "-#{key}") || []).include?(value)
else
(params.dig(inclusive_filters_key, key) || []).to_set == value.to_set
end
when Blacklight::SearchState::FilterField::MISSING
(params.dig(filters_key, "-#{key}") || []).include?(Blacklight::Engine.config.blacklight.facet_missing_param)
else
Expand Down
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 @@ -134,7 +134,9 @@ def add_facet_fq_to_solr(solr_parameters)
solr_parameters.merge!(subqueries) if subqueries
else
filter.values.compact_blank.each do |value|
filter_query, subqueries = if value.is_a?(Array)
filter_query, subqueries = if value.is_a?(Array) && value.include?(Blacklight::SearchState::FilterField::EXCLUDE)
facet_value_to_fq_string(filter.config.key, value.first, exclude: true)
elsif value.is_a?(Array)
facet_inclusive_value_to_fq_string(filter.key, value.compact_blank)
else
facet_value_to_fq_string(filter.config.key, value)
Expand Down Expand Up @@ -333,7 +335,7 @@ def solr_param_quote(val, options = {})
##
# Convert a facet/value pair into a solr fq parameter
# rubocop:disable Metrics/PerceivedComplexity
def facet_value_to_fq_string(facet_field, value, use_local_params: true)
def facet_value_to_fq_string(facet_field, value, use_local_params: true, exclude: false)
facet_config = blacklight_config.facet_fields[facet_field]

solr_field = facet_config.field if facet_config && !facet_config.query
Expand All @@ -355,7 +357,7 @@ def facet_value_to_fq_string(facet_field, value, use_local_params: true)
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)}"
"#{'-' if exclude}{!term f=#{solr_field}#{" #{local_params.join(' ')}" unless local_params.empty?}}#{convert_to_term_value(value)}"
end
end
# rubocop:enable Metrics/PerceivedComplexity
Expand Down