Skip to content

Commit

Permalink
Merge pull request #146 from alphagov/more-filters
Browse files Browse the repository at this point in the history
Add `content_purpose_supergroup` filter
  • Loading branch information
csutter authored Dec 11, 2023
2 parents d9b88a6 + 2351f03 commit af11e8f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
26 changes: 23 additions & 3 deletions app/services/discovery_engine/query/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def initialize(query_params)
end

def filter_expression
reject_links_filter
surround_and_join([
reject_links_filter,
content_purpose_supergroup_filter,
], between: " AND ", surround: "(", surround_end: ")")
end

private
Expand All @@ -15,8 +18,25 @@ def filter_expression
def reject_links_filter
return nil if query_params[:reject_link].blank?

reject_links = Array(query_params[:reject_link]).map { "\"#{_1}\"" }.join(",")
"NOT link: ANY(#{reject_links})"
values = surround_and_join(query_params[:reject_link], between: ",", surround: '"')
"NOT link: ANY(#{values})"
end

def content_purpose_supergroup_filter
return nil if query_params[:filter_content_purpose_supergroup].blank?

values = surround_and_join(
query_params[:filter_content_purpose_supergroup], between: ",", surround: '"'
)
"content_purpose_supergroup: ANY(#{values})"
end

def surround_and_join(string_or_strings, between:, surround:, surround_end: surround)
Array(string_or_strings)
.compact
.map { "#{surround}#{_1}#{surround_end}" }
.join(between)
.presence
end
end
end
46 changes: 38 additions & 8 deletions spec/services/discovery_engine/query/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,50 @@
it { is_expected.to be_nil }
end

context "with a single reject_link parameter" do
let(:query_params) { { q: "garden centres", reject_link: "/foo" } }
context "with reject_link" do
context "with an empty parameter" do
let(:query_params) { { q: "garden centres", reject_link: "" } }

it "calls the client with the expected parameters" do
expect(filter_expression).to eq('NOT link: ANY("/foo")')
it { is_expected.to be_nil }
end

context "with a single parameter" do
let(:query_params) { { q: "garden centres", reject_link: "/foo" } }

it { is_expected.to eq('(NOT link: ANY("/foo"))') }
end

context "with several parameters" do
let(:query_params) { { q: "garden centres", reject_link: ["/foo", "/bar"] } }

it { is_expected.to eq('(NOT link: ANY("/foo","/bar"))') }
end
end

context "with several reject_link parameter" do
let(:query_params) { { q: "garden centres", reject_link: ["/foo", "/bar"] } }
context "with filter_content_purpose_supergroup" do
context "with an empty parameter" do
let(:query_params) { { q: "garden centres", filter_content_purpose_supergroup: "" } }

it "calls the client with the expected parameters" do
expect(filter_expression).to eq('NOT link: ANY("/foo","/bar")')
it { is_expected.to be_nil }
end

context "with a single parameter" do
let(:query_params) { { q: "garden centres", filter_content_purpose_supergroup: "services" } }

it { is_expected.to eq('(content_purpose_supergroup: ANY("services"))') }
end

context "with several parameters" do
let(:query_params) { { q: "garden centres", filter_content_purpose_supergroup: %w[services guidance] } }

it { is_expected.to eq('(content_purpose_supergroup: ANY("services","guidance"))') }
end
end

context "with several filters specified" do
let(:query_params) { { q: "garden centres", reject_link: "/foo", filter_content_purpose_supergroup: "services" } }

it { is_expected.to eq('(NOT link: ANY("/foo")) AND (content_purpose_supergroup: ANY("services"))') }
end
end
end

0 comments on commit af11e8f

Please sign in to comment.