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

Use v2 for all site search queries #3295

Merged
merged 1 commit into from
Feb 28, 2024
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
7 changes: 6 additions & 1 deletion app/lib/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# search results from rummager.
module Search
class Query
SITE_SEARCH_FINDER_BASE_PATH = "/search/all".freeze

include ActiveModel::Validations

attr_reader :filter_params
Expand Down Expand Up @@ -110,7 +112,10 @@ def fetch_search_response(content_item)
end

def use_v2_api?
ActiveModel::Type::Boolean.new.cast(filter_params["use_v2"]) || ab_params[:vertex] == "B"
return false if ActiveModel::Type::Boolean.new.cast(filter_params["use_v1"])
return true if ActiveModel::Type::Boolean.new.cast(filter_params["use_v2"])

content_item.base_path == SITE_SEARCH_FINDER_BASE_PATH
end
end
end
11 changes: 11 additions & 0 deletions features/step_definitions/filtering_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@
order: "-public_timestamp",
)).to_return(body: all_content_manuals_results_json)

stub_request(:get, DocumentHelper::SEARCH_V2_ENDPOINT)
.with(query: hash_including(q: "Replacing bristles", order: "-public_timestamp"))
.to_return(body: all_content_results_json)

stub_request(:get, DocumentHelper::SEARCH_V2_ENDPOINT)
.with(query: hash_including(
filter_manual: "/guidance/care-and-use-of-a-nimbus-2000",
q: "Replacing bristles",
order: "-public_timestamp",
)).to_return(body: all_content_manuals_results_json)

visit finder_path("search/all", manual: "/guidance/care-and-use-of-a-nimbus-2000", q: "Replacing bristles")
end

Expand Down
23 changes: 23 additions & 0 deletions features/support/document_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module DocumentHelper
include GdsApi::TestHelpers::Worldwide

SEARCH_ENDPOINT = "#{Plek.find('search-api')}/search.json".freeze
SEARCH_V2_ENDPOINT = "#{Plek.find('search-api-v2')}/search.json".freeze

def stub_taxonomy_api_request
stub_content_store_has_item("/", "links" => { "level_one_taxons" => [] })
Expand Down Expand Up @@ -51,6 +52,12 @@ def stub_search_api_request_with_query_param_no_results(query)
.to_return(
body: no_results_json,
)

stub_request(:get, SEARCH_V2_ENDPOINT)
.with(query: hash_including("q" => query))
.to_return(
body: no_results_json,
)
end

def stub_search_api_request_with_10_government_results
Expand Down Expand Up @@ -91,24 +98,40 @@ def stub_search_api_request_with_all_content_results
stub_request(:get, SEARCH_ENDPOINT)
.with(query: hash_including(all_content_params.merge(order: "-public_timestamp")))
.to_return(body: all_content_results_json)

stub_request(:get, SEARCH_V2_ENDPOINT)
.with(query: hash_including(all_content_params.merge(order: "-public_timestamp")))
.to_return(body: all_content_results_json)
end

def stub_search_api_request_with_organisation_filter_all_content_results
stub_request(:get, SEARCH_ENDPOINT)
.with(query: hash_including("q" => "search-term", "filter_organisations" => %w[ministry-of-magic]))
.to_return(body: filtered_by_organisation_all_content_results_json)

stub_request(:get, SEARCH_V2_ENDPOINT)
.with(query: hash_including("q" => "search-term", "filter_organisations" => %w[ministry-of-magic]))
.to_return(body: filtered_by_organisation_all_content_results_json)
end

def stub_search_api_request_with_misspelt_query
stub_request(:get, SEARCH_ENDPOINT)
.with(query: hash_including("q" => "drving"))
.to_return(body: spelling_suggestions_json)

stub_request(:get, SEARCH_V2_ENDPOINT)
.with(query: hash_including("q" => "drving"))
.to_return(body: spelling_suggestions_json)
end

def stub_search_api_request_with_manual_filter_all_content_results
stub_request(:get, SEARCH_ENDPOINT)
.with(query: hash_including("q" => "search-term", "filter_manual" => %w[how-to-be-a-wizard]))
.to_return(body: filtered_by_manual_all_content_results_json)

stub_request(:get, SEARCH_V2_ENDPOINT)
.with(query: hash_including("q" => "search-term", "filter_manual" => %w[how-to-be-a-wizard]))
.to_return(body: filtered_by_manual_all_content_results_json)
end

def stub_search_api_request_with_filtered_policy_papers_results
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/finders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@

describe "with legacy query parameters for publications" do
before do
search_api_request
search_api_request(search_api_app: "search-api-v2")
stub_content_store_has_item("/search/all", all_content_finder)

@default_params = { slug: "search/all" }
Expand Down
41 changes: 39 additions & 2 deletions spec/lib/search/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def stub_batch_search
"details" => {
"facets" => facets,
},
"base_path" => "/some/finder",
)
end

Expand Down Expand Up @@ -49,6 +50,33 @@ def result_item(id, title, score:, popularity:, updated:)
}
end

context "when manually overriding parameters to use the v1 API" do
subject { described_class.new(content_item, { "use_v1" => "true" }).search_results }

let(:content_item) do
ContentItem.new({
"base_path" => "/search/all",
"details" => {
"facets" => facets,
},
})
end

before do
stub_search.to_return(body: {
"results" => [
result_item("/i-am-the-v1-api", "I am the v1 API", score: nil, updated: "14-12-19", popularity: 1),
],
}.to_json)
end

it "calls the v1 API" do
results = subject.fetch("results")
expect(results.length).to eq(1)
expect(results.first).to match(hash_including("_id" => "/i-am-the-v1-api"))
end
end

context "when manually overriding parameters to use the v2 API" do
subject { described_class.new(content_item, { "use_v2" => "true" }).search_results }

Expand All @@ -67,8 +95,17 @@ def result_item(id, title, score:, popularity:, updated:)
end
end

context "when in AB test variant B" do
subject { described_class.new(content_item, {}, ab_params: { vertex: "B" }).search_results }
context "when on the site search finder" do
subject { described_class.new(content_item, {}).search_results }

let(:content_item) do
ContentItem.new({
"base_path" => "/search/all",
"details" => {
"facets" => facets,
},
})
end

before do
stub_search_v2.to_return(body: {
Expand Down
Loading