Skip to content

Commit c666d8d

Browse files
committed
Refactor DiscoveryEngine::Search
- Pass entirety of query parameters to search (in anticipation of implementing more querying functionality) - Move to a more OO-y rather than "callable" structure for the class and move parameters into object attributes - Add general `permit` of params to Brakeman ignore file
1 parent 6381386 commit c666d8d

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
class SearchesController < ApplicationController
22
def show
3-
result_set = DiscoveryEngine::Search.new.call(
4-
query_params[:q],
5-
start: query_params[:start]&.to_i,
6-
count: query_params[:count]&.to_i,
7-
)
8-
9-
render json: result_set
3+
render json: DiscoveryEngine::Search.new(query_params).result_set
104
end
115

126
private
137

148
def query_params
15-
params.permit(:q, :start, :count)
9+
params.permit!
1610
end
1711
end

app/services/discovery_engine/search.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
module DiscoveryEngine
22
class Search
3-
DEFAULT_COUNT = 10
4-
DEFAULT_START = 0
3+
DEFAULT_PAGE_SIZE = 10
4+
DEFAULT_OFFSET = 0
55

66
include BestBetsBoost
77
include NewsRecencyBoost
88

9-
def initialize(client: ::Google::Cloud::DiscoveryEngine.search_service(version: :v1))
9+
def initialize(
10+
query_params,
11+
client: ::Google::Cloud::DiscoveryEngine.search_service(version: :v1)
12+
)
13+
@query_params = query_params
1014
@client = client
1115
end
1216

13-
def call(query_string, start: nil, count: nil)
14-
count ||= DEFAULT_COUNT
15-
start ||= DEFAULT_START
16-
query_string ||= ""
17-
17+
def result_set
1818
response = client.search(
19-
query: query_string,
19+
query:,
2020
serving_config:,
21-
page_size: count,
22-
offset: start,
23-
boost_spec: boost_spec(query_string),
21+
page_size:,
22+
offset:,
23+
boost_spec:,
2424
).response
2525

2626
ResultSet.new(
2727
results: response.results.map { Result.from_stored_document(_1.document.struct_data.to_h) },
2828
total: response.total_size,
29-
start:,
29+
start: offset,
3030
)
3131
end
3232

3333
private
3434

35-
attr_reader :client
35+
attr_reader :query_params, :client
36+
37+
def query
38+
query_params[:q].presence || ""
39+
end
40+
41+
def page_size
42+
query_params[:count].presence&.to_i || DEFAULT_PAGE_SIZE
43+
end
44+
45+
def offset
46+
query_params[:start].presence&.to_i || DEFAULT_OFFSET
47+
end
3648

3749
def serving_config
3850
Rails.configuration.discovery_engine_serving_config
3951
end
4052

41-
def boost_spec(query_string)
53+
def boost_spec
4254
{
4355
condition_boost_specs: [
4456
*news_recency_boost_specs,
45-
*best_bets_boost_specs(query_string),
57+
*best_bets_boost_specs(query),
4658
],
4759
}
4860
end

config/brakeman.ignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"ignored_warnings": [
3+
{
4+
"warning_type": "Mass Assignment",
5+
"warning_code": 70,
6+
"fingerprint": "4c13d7998d2abdaad68d35c884f9a4527c1c3085beda87c99a93ada0eaef496e",
7+
"check_name": "MassAssignment",
8+
"message": "Specify exact keys allowed for mass assignment instead of using `permit!` which allows any keys",
9+
"file": "app/controllers/searches_controller.rb",
10+
"line": 9,
11+
"link": "https://brakemanscanner.org/docs/warning_types/mass_assignment/",
12+
"code": "params.permit!",
13+
"render_path": null,
14+
"location": {
15+
"type": "method",
16+
"class": "SearchesController",
17+
"method": "query_params"
18+
},
19+
"user_input": null,
20+
"confidence": "Medium",
21+
"cwe_id": [
22+
915
23+
],
24+
"note": "Mass assignment not a risk as not using Active Record and parsing these parameters elsewhere"
25+
}
26+
],
27+
"updated": "2023-12-06 16:07:36 +0000",
28+
"brakeman_version": "6.1.0"
29+
}

spec/requests/search_request_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RSpec.describe "Making a search request" do
2-
let(:search_service) { instance_double(DiscoveryEngine::Search, call: result_set) }
2+
let(:search_service) { instance_double(DiscoveryEngine::Search, result_set:) }
33
let(:result_set) { ResultSet.new(results:, total: 42, start: 21) }
44
let(:results) { [Result.new(content_id: "123"), Result.new(content_id: "456")] }
55

@@ -25,7 +25,9 @@
2525
it "passes any query parameters to the search service in the expected format" do
2626
get "/search.json?q=garden+centres&start=11&count=22"
2727

28-
expect(search_service).to have_received(:call).with("garden centres", start: 11, count: 22)
28+
expect(DiscoveryEngine::Search).to have_received(:new).with(
29+
hash_including(q: "garden centres", start: "11", count: "22"),
30+
)
2931
end
3032
end
3133
end

spec/services/discovery_engine/search_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RSpec.describe DiscoveryEngine::Search do
2-
subject(:search) { described_class.new(client:) }
2+
subject(:search) { described_class.new(query_params, client:) }
33

44
let(:client) { double("SearchService::Client", search: search_return_value) }
55

@@ -25,9 +25,11 @@
2525
end
2626
end
2727

28-
describe "#call" do
28+
describe "#result_set" do
29+
subject!(:result_set) { search.result_set }
30+
2931
context "when the search is successful" do
30-
subject!(:result_set) { search.call("garden centres") }
32+
let(:query_params) { { q: "garden centres" } }
3133

3234
let(:search_return_value) { double(response: search_response) }
3335
let(:search_response) { double(total_size: 42, results:) }
@@ -58,7 +60,7 @@
5860
end
5961

6062
context "when start and count are specified" do
61-
subject!(:result_set) { search.call("garden centres", start: 11, count: 22) }
63+
let(:query_params) { { q: "garden centres", start: "11", count: "22" } }
6264

6365
it "calls the client with the expected parameters" do
6466
expect(client).to have_received(:search).with(
@@ -77,7 +79,7 @@
7779

7880
context "when searching for a query that has a single best bets defined" do
7981
# see test section in YAML config
80-
subject!(:result_set) { search.call("i want to test a single best bet") }
82+
let(:query_params) { { q: "i want to test a single best bet" } }
8183

8284
let(:expected_boost_specs) do
8385
super() + [{
@@ -99,7 +101,7 @@
99101

100102
context "when searching for a query that has multiple best bets defined" do
101103
# see test section in YAML config
102-
subject!(:result_set) { search.call("i want to test multiple best bets") }
104+
let(:query_params) { { q: "i want to test multiple best bets" } }
103105

104106
let(:expected_boost_specs) do
105107
super() + [{

0 commit comments

Comments
 (0)