Skip to content

Commit

Permalink
fix: remove default sort on name of an API in the search endpoint
Browse files Browse the repository at this point in the history
When doing a search we want to rely on Lucene indexes scores to have most relevant APIs firsts in the list.

https://gravitee.atlassian.net/browse/APIM-1991
gravitee-io/issues#9095
  • Loading branch information
gaetanmaisse authored and ytvnr committed Jul 4, 2023
1 parent e3ec529 commit fbfebce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class ApisController {
$timeout.cancel(this.timer);
this.timer = $timeout(() => {
if (query !== undefined && query !== previousQuery) {
this.order = null;
this.searchWithQuery(query);
}
}, 300);
Expand Down
2 changes: 1 addition & 1 deletion gravitee-apim-console-webui/src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class ApiService {
opts.params = {
q: query ? query : '*',
page: page,
order: order,
...(order ? { order: order } : {}),
size,
...(manageOnly ? {} : { manageOnly: false }),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public PagedResult<ApiListItem> searchPagedApis(
implementation = String.class,
description = "By default, sort is ASC. If *field* starts with '-', the order sort is DESC. Currently, only **name** and **paths** are supported"
)
) @QueryParam("order") @DefaultValue("name") final ApisOrderParam apisOrderParam,
) @QueryParam("order") final ApisOrderParam apisOrderParam,
@Parameter(
name = "manageOnly",
description = "By default only APIs that the user can manage are returned. If set to false, all APIs that the user can view are returned."
Expand All @@ -459,7 +459,13 @@ public PagedResult<ApiListItem> searchPagedApis(

final boolean isRatingServiceEnabled = ratingService.isEnabled(executionContext);

final Page<ApiEntity> apis = apiService.search(executionContext, query, filters, apisOrderParam.toSortable(), commonPageable);
final Page<ApiEntity> apis = apiService.search(
executionContext,
query,
filters,
apisOrderParam != null ? apisOrderParam.toSortable() : null,
commonPageable
);

return new PagedResult<>(
apis.getContent().stream().map(apiEntity -> this.convert(apiEntity, isRatingServiceEnabled)).collect(toList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void get_should_search_apis() throws TechnicalException {
eq(GraviteeContext.getExecutionContext()),
eq("*"),
argThat(filters -> ((Set<String>) filters.get("api")).size() == 3),
isA(Sortable.class),
isNull(),
isA(Pageable.class)
)
)
Expand All @@ -72,6 +72,29 @@ public void get_should_search_apis() throws TechnicalException {
assertEquals(OK_200, response.getStatus());
}

@Test
public void get_should_search_apis_with_order() throws TechnicalException {
when(apiService.findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), isA(ApiQuery.class), eq(true)))
.thenReturn(Set.of("api1", "api2", "api15"));

List<ApiEntity> resultApis = List.of(mockApi("api1"), mockApi("api2"), mockApi("api15"));
Page<ApiEntity> apisPage = new Page<>(resultApis, 7, 3, 54);
when(
apiService.search(
eq(GraviteeContext.getExecutionContext()),
eq("*"),
argThat(filters -> ((Set<String>) filters.get("api")).size() == 3),
isA(Sortable.class),
isA(Pageable.class)
)
)
.thenReturn(apisPage);

final Response response = envTarget().path("_search/_paged").queryParam("q", "*").queryParam("order", "name").request().post(null);

assertEquals(OK_200, response.getStatus());
}

@Test
public void get_should_search_apis_with_manageOnly_to_false() throws TechnicalException {
when(apiService.findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), isA(ApiQuery.class), eq(false)))
Expand All @@ -85,7 +108,7 @@ public void get_should_search_apis_with_manageOnly_to_false() throws TechnicalEx
eq(GraviteeContext.getExecutionContext()),
eq("*"),
argThat(filters -> ((Set<String>) filters.get("api")).size() == 3),
isA(Sortable.class),
isNull(),
isA(Pageable.class)
)
)
Expand Down

0 comments on commit fbfebce

Please sign in to comment.