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

Add content_purpose_supergroup filter #146

Merged
merged 1 commit into from
Dec 11, 2023
Merged
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
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