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

III-6275 Make Labels findable by using Levenshtein distance #1837

Closed
wants to merge 7 commits into from
Closed
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
6 changes: 3 additions & 3 deletions features/label/get.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Feature: Test the UDB3 labels API
And the response body should be valid JSON
And the JSON response at "totalItems" should be 4
And the JSON response at "member/0/name" should be "special_label"
And the JSON response at "member/1/name" should be "special_label*"
And the JSON response at "member/2/name" should be "special_label#"
And the JSON response at "member/3/name" should be "special-label"
And the JSON response at "member/1/name" should be "special-label"
And the JSON response at "member/2/name" should be "special_label*"
And the JSON response at "member/3/name" should be "special_label#"

Scenario: Hide excluded labels if suggestion is true
When I create a label with a random name of 10 characters
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Label/SearchLabelsRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface

$totalEntities = $this->labelRepository->searchTotalLabels($query);

$entities = $totalEntities > 0 ? $this->labelRepository->search($query) : [];
$entities = $totalEntities > 0 ? $this->labelRepository->searchByLevenshtein($query) : [];

return new PagedCollectionResponse(
$query->getLimit() ?? 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function search(Query $query): array
return $this->repository->search($query);
}

public function searchByLevenshtein(Query $query): array
{
return $this->repository->searchByLevenshtein($query);
}

public function searchTotalLabels(Query $query): int
{
return $this->repository->searchTotalLabels($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ public function search(Query $query): array
return $this->getResults($queryBuilder);
}

/**
* @return Entity[]
*/
public function searchByLevenshtein(Query $query): array
{
$entities = $this->search($query);

$reference = $query->getValue();
usort($entities, function ($entityA, $entityB) use ($reference) {
$distanceA = levenshtein($reference, $entityA->getName());
$distanceB = levenshtein($reference, $entityB->getName());

return $distanceA - $distanceB;
});

return $entities;
}

public function searchTotalLabels(Query $query): int
{
$queryBuilder = $this->createSearchQuery($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function search(Query $query): array
return $this->repository->search($query);
}

public function searchByLevenshtein(Query $query): array
{
return $this->repository->searchByLevenshtein($query);
}

public function searchTotalLabels(Query $query): int
{
return $this->repository->searchTotalLabels($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public function canUseLabel(string $userId, string $name): bool;

public function search(Query $query): array;

public function searchByLevenshtein(Query $query): array;

public function searchTotalLabels(Query $query): int;
}
2 changes: 1 addition & 1 deletion tests/Http/Label/SearchLabelsRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function it_can_search_labels(): void
->willReturn(count($this->labels));

$this->labelRepository->expects($this->once())
->method('search')
->method('searchByLevenshtein')
->with(new Query('label', '123', 5, 2))
->willReturn($this->labels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,52 @@ public function it_filters_excluded_labels_if_suggestions_is_true(): void
$this->assertEquals([], $entities);
}

/**
* @test
*/
public function it_gets_labels_alphabetically_by_default(): void
{
$search = new Query(
'wandel',
null,
null,
null,
true
);
$entities = $this->dbalReadRepository->search($search);
$this->assertEquals(
[
$this->entityByName,
$this->entityPrivateNoAccess,
$this->entityPrivateAccess,
],
$entities
);
}

/**
* @test
*/
public function it_can_get_labels_levenshtein_distance(): void
{
$search = new Query(
'wandel',
null,
null,
null,
true
);
$entities = $this->dbalReadRepository->searchByLevenshtein($search);
$this->assertEquals(
[
$this->entityPrivateAccess,
$this->entityByName,
$this->entityPrivateNoAccess,
],
$entities
);
}

/**
* @test
*/
Expand Down
Loading