Skip to content

Commit

Permalink
WIP - Add new endpoint to query specialist-finder index
Browse files Browse the repository at this point in the history
  • Loading branch information
minhngocd committed Nov 4, 2024
1 parent 41a0593 commit 29b0c2f
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 25 deletions.
21 changes: 21 additions & 0 deletions lib/rummager/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ def json_only
halt(500, env["sinatra.error"].message)
end

# Return results for the Specialist Finder searches
#
# For details, see docs/search-api.md
["/specialist-documents-search.?:request_format?", "/api/specialist-documents-search.?:request_format?"].each do |path|
get path do
json_only

query_params = parse_query_string(request.query_string)

begin
results = SearchConfig.run_specialist_document_search(query_params)
rescue BaseParameterParser::ParseError => e
status 422
return { error: e.error }.to_json
end

headers["Access-Control-Allow-Origin"] = "*"
results.to_json
end
end

# Return results for the GOV.UK site search
#
# For details, see docs/search-api.md
Expand Down
15 changes: 14 additions & 1 deletion lib/search/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,26 @@ def query
end

def filter
return specialist_documents_post_filter if content_index_names.include?(SearchConfig.specialist_finder_index_name)

Search::FormatMigrator.new(
search_params.search_config,
base_query: QueryComponents::Filter.new(search_params).payload,
).call
end

private
def specialist_documents_post_filter
{ bool:
{
minimum_should_match: 1,
should: [{
bool: { must: QueryComponents::Filter.new(search_params).payload }

Check failure on line 80 in lib/search/query_builder.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
}],
},
}

Check failure on line 83 in lib/search/query_builder.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/MultilineHashBraceLayout: Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.
end

private

Check failure on line 86 in lib/search/query_builder.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/AccessModifierIndentation: Outdent access modifiers like `private`. (https://rubystyle.guide#indent-public-private-protected)

attr_reader :content_index_names, :metasearch_index

Expand Down
24 changes: 23 additions & 1 deletion lib/search_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def run_search(raw_parameters)
search_params.search_config.run_search_with_params(search_params)
end

def run_specialist_document_search(raw_parameters)
search_params = parse_parameters(raw_parameters)
search_params.search_config.run_specialist_document_search_with_params(search_params)
end

def run_batch_search(searches)
search_params = []
searches.each do |search|
Expand Down Expand Up @@ -106,7 +111,7 @@ def search_server
SearchConfig.index_names,
SearchConfig.govuk_index_name,
SearchConfig.content_index_names,
self,
self

Check failure on line 114 in lib/search_config.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInArguments: Put a comma after the last parameter of a multiline method call. (https://rubystyle.guide#no-trailing-params-comma)
)
end

Expand All @@ -121,6 +126,10 @@ def run_search_with_params(search_params)
searcher.run(search_params)
end

def run_specialist_document_search_with_params(search_params)
specialist_document_searcher.run(search_params)
end

def run_batch_search_with_params(search_params)
batch_searcher.run(search_params)
end
Expand Down Expand Up @@ -149,6 +158,10 @@ def new_content_index
@new_content_index ||= search_server.index_for_search([SearchConfig.govuk_index_name])
end

def specialist_documents_content_index
@specialist_documents_content_index ||= search_server.index_for_search(SearchConfig.content_index_names + [SearchConfig.specialist_finder_index_name])
end

def base_uri
cluster.uri
end
Expand Down Expand Up @@ -177,6 +190,15 @@ def searcher
)
end

def specialist_document_searcher
@specialist_document_searcher ||= Search::Query.new(
content_index: specialist_documents_content_index,
registries:,
metasearch_index:,
spelling_index:,
)
end

def batch_searcher
@batch_searcher ||= Search::BatchQuery.new(
content_index:,
Expand Down
3 changes: 2 additions & 1 deletion lib/search_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(base_uri, schema, index_names, govuk_index_name, content_index_na
@govuk_index_name = govuk_index_name
@content_index_names = content_index_names
@search_config = search_config
@specialist_finder_index_name = SearchConfig.specialist_finder_index_name
end

def index_group(prefix)
Expand Down Expand Up @@ -52,7 +53,7 @@ def validate_index_name!(index_name)

def index_name_valid?(index_name)
index_name.split(",").all? do |name|
@index_names.include?(name) || @govuk_index_name == name
@index_names.include?(name) || @govuk_index_name == name || @specialist_finder_index_name == name
end
end
end
Expand Down
Loading

0 comments on commit 29b0c2f

Please sign in to comment.