-
Notifications
You must be signed in to change notification settings - Fork 1
feat(service): relevance-based sorting for stop search #338
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/org/naviqore/app/dto/StopSortStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.naviqore.app.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum StopSortStrategy { | ||
|
||
ALPHABETICAL("ALPHABETICAL"), | ||
RELEVANCE("RELEVANCE"); | ||
|
||
private final String value; | ||
|
||
@JsonCreator | ||
public static StopSortStrategy fromValue(String value) { | ||
for (StopSortStrategy b : StopSortStrategy.values()) { | ||
if (b.value.equals(value)) { | ||
return b; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unexpected value '" + value + "'"); | ||
} | ||
|
||
@JsonValue | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
libs/public-transit-service/src/main/java/org/naviqore/service/StopSortStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.naviqore.service; | ||
|
||
import org.naviqore.gtfs.schedule.model.Stop; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* Sorting strategies for stop search results. | ||
*/ | ||
public enum StopSortStrategy { | ||
|
||
/** | ||
* Sorts stops alphabetically by name. | ||
*/ | ||
ALPHABETICAL { | ||
@Override | ||
public Comparator<Stop> getComparator(String query) { | ||
return Comparator.comparing(Stop::getName); | ||
} | ||
}, | ||
|
||
/** | ||
* Sorts stops by relevance to the search query. The relevance is determined by: | ||
* <ul> | ||
* <li>Exact match (score 0)</li> | ||
* <li>Starts with the query (score 1)</li> | ||
* <li>Contains the query (score 2)</li> | ||
* </ul> | ||
* Tie-breaking is done by name length (shorter is better), then alphabetically. | ||
*/ | ||
RELEVANCE { | ||
@Override | ||
public Comparator<Stop> getComparator(String query) { | ||
String lowerCaseQuery = query.toLowerCase(); | ||
|
||
return (s1, s2) -> { | ||
String name1 = s1.getName().toLowerCase(); | ||
String name2 = s2.getName().toLowerCase(); | ||
|
||
int score1 = calculateScore(name1, lowerCaseQuery); | ||
int score2 = calculateScore(name2, lowerCaseQuery); | ||
|
||
// primary sort: by relevance score (lower is better) | ||
if (score1 != score2) { | ||
return Integer.compare(score1, score2); | ||
} | ||
|
||
// secondary sort: by name length (shorter is better) | ||
munterfi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (name1.length() != name2.length()) { | ||
return Integer.compare(name1.length(), name2.length()); | ||
} | ||
|
||
// tertiary sort: alphabetically | ||
return s1.getName().compareTo(s2.getName()); | ||
}; | ||
} | ||
|
||
private int calculateScore(String name, String query) { | ||
if (name.equals(query)) { | ||
return 0; | ||
} | ||
if (name.startsWith(query)) { | ||
return 1; | ||
} | ||
|
||
return 2; | ||
} | ||
}; | ||
|
||
/** | ||
* Get the comparator for the specific strategy. | ||
*/ | ||
public abstract Comparator<Stop> getComparator(String query); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
libs/public-transit-service/src/test/java/org/naviqore/service/StopSortStrategyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.naviqore.service; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.naviqore.gtfs.schedule.model.Stop; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class StopSortStrategyTest { | ||
|
||
@Mock | ||
private Stop stopGstaad; | ||
@Mock | ||
private Stop stopGstaadBahnhof; | ||
@Mock | ||
private Stop stopGrundGstaad; | ||
@Mock | ||
private Stop stopAnother; | ||
@Mock | ||
private Stop stopGstaadDuplicate; | ||
|
||
private List<Stop> testStops; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(stopGstaad.getName()).thenReturn("Gstaad"); | ||
munterfi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
when(stopGstaadBahnhof.getName()).thenReturn("Gstaad, Bahnhof"); | ||
when(stopGrundGstaad.getName()).thenReturn("Grund b. Gstaad"); | ||
when(stopAnother.getName()).thenReturn("Another Place"); | ||
when(stopGstaadDuplicate.getName()).thenReturn("Gstaad"); | ||
|
||
testStops = new ArrayList<>( | ||
List.of(stopGstaad, stopGstaadBahnhof, stopGrundGstaad, stopAnother, stopGstaadDuplicate)); | ||
|
||
Collections.shuffle(testStops); | ||
} | ||
|
||
@Test | ||
void getComparator_withRelevanceSort_shouldOrderByScoreLengthThenName() { | ||
String query = "Gstaa"; | ||
Comparator<Stop> comparator = StopSortStrategy.RELEVANCE.getComparator(query); | ||
|
||
List<String> expectedOrder = List.of("Gstaad", "Gstaad", "Gstaad, Bahnhof", "Grund b. Gstaad"); | ||
|
||
List<String> actualOrder = testStops.stream() | ||
.filter(s -> s.getName().toLowerCase().contains(query.toLowerCase())) | ||
.sorted(comparator) | ||
.map(Stop::getName) | ||
.collect(Collectors.toList()); | ||
|
||
assertIterableEquals(expectedOrder, actualOrder); | ||
} | ||
|
||
@Test | ||
void getComparator_withExactMatchQuery_shouldPlaceExactMatchFirst() { | ||
String query = "Gstaad"; | ||
Comparator<Stop> comparator = StopSortStrategy.RELEVANCE.getComparator(query); | ||
|
||
List<String> expectedOrder = List.of("Gstaad", "Gstaad", "Gstaad, Bahnhof", "Grund b. Gstaad"); | ||
|
||
List<String> actualOrder = testStops.stream() | ||
.filter(s -> s.getName().toLowerCase().contains(query.toLowerCase())) | ||
.sorted(comparator) | ||
.map(Stop::getName) | ||
.collect(Collectors.toList()); | ||
|
||
assertIterableEquals(expectedOrder, actualOrder); | ||
} | ||
|
||
@Test | ||
void getComparator_withMixedCaseQuery_shouldBeCaseInsensitive() { | ||
String query = "gStAaD"; | ||
Comparator<Stop> comparator = StopSortStrategy.RELEVANCE.getComparator(query); | ||
|
||
List<String> expectedOrder = List.of("Gstaad", "Gstaad", "Gstaad, Bahnhof", "Grund b. Gstaad"); | ||
|
||
List<String> actualOrder = testStops.stream() | ||
.filter(s -> s.getName().toLowerCase().contains(query.toLowerCase())) | ||
.sorted(comparator) | ||
.map(Stop::getName) | ||
.collect(Collectors.toList()); | ||
|
||
assertIterableEquals(expectedOrder, actualOrder); | ||
} | ||
|
||
@Test | ||
void getComparator_withAlphabeticalSort_shouldOrderByNameOnly() { | ||
String query = "any"; | ||
Comparator<Stop> comparator = StopSortStrategy.ALPHABETICAL.getComparator(query); | ||
|
||
List<String> expectedOrder = List.of("Another Place", "Grund b. Gstaad", "Gstaad", "Gstaad", "Gstaad, Bahnhof"); | ||
|
||
List<String> actualOrder = testStops.stream() | ||
.sorted(comparator) | ||
.map(Stop::getName) | ||
.collect(Collectors.toList()); | ||
|
||
assertIterableEquals(expectedOrder, actualOrder); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.