fix: Set min query length in the search API endpoints (#10698)

pull/10805/head
Victor Baranov 2 months ago committed by GitHub
parent e68090523c
commit 78ebdf88e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 22
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/search_controller.ex
  2. 12
      apps/block_scout_web/lib/block_scout_web/controllers/search_controller.ex

@ -8,6 +8,16 @@ defmodule BlockScoutWeb.API.V2.SearchController do
alias Explorer.PagingOptions
@api_true [api?: true]
@min_query_length 3
def search(conn, %{"q" => query}) when byte_size(query) < @min_query_length do
conn
|> put_status(200)
|> render(:search_results, %{
search_results: [],
next_page_params: nil
})
end
def search(conn, %{"q" => query} = params) do
[paging_options: paging_options] = paging_options(params)
@ -29,6 +39,12 @@ defmodule BlockScoutWeb.API.V2.SearchController do
})
end
def check_redirect(conn, %{"q" => query}) when byte_size(query) < @min_query_length do
conn
|> put_status(200)
|> render(:search_results, %{result: {:error, :not_found}})
end
def check_redirect(conn, %{"q" => query}) do
result =
query
@ -40,6 +56,12 @@ defmodule BlockScoutWeb.API.V2.SearchController do
|> render(:search_results, %{result: result})
end
def quick_search(conn, %{"q" => query}) when byte_size(query) < @min_query_length do
conn
|> put_status(200)
|> render(:search_results, %{search_results: []})
end
def quick_search(conn, %{"q" => query}) do
search_results = Search.balanced_unpaginated_search(%PagingOptions{page_size: 50}, query, @api_true)

@ -7,6 +7,18 @@ defmodule BlockScoutWeb.SearchController do
alias Explorer.Chain.Search
alias Phoenix.View
@min_query_length 3
def search_results(conn, %{"q" => query, "type" => "JSON"}) when byte_size(query) < @min_query_length do
json(
conn,
%{
items: [],
next_page_path: nil
}
)
end
def search_results(conn, %{"q" => query, "type" => "JSON"} = params) do
[paging_options: paging_options] = paging_options(params)
offset = (max(paging_options.page_number, 1) - 1) * paging_options.page_size

Loading…
Cancel
Save