-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented first version of FullSearchQueryParser to tokemize user input
- Loading branch information
Showing
8 changed files
with
92 additions
and
12 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
server/src/main/java/access/api/FullSearchQueryParser.java
This file contains 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,38 @@ | ||
package access.api; | ||
|
||
import access.exception.InvalidInputException; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class FullSearchQueryParser { | ||
|
||
//SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORD; | ||
private static final List<String> stopWords = List.of( | ||
"a", "about", "an", "are", | ||
"as", "at", "be", "by", | ||
"com", "de", "en", "for", | ||
"from", "how", "i", "in", | ||
"is", "it", "la", "of", | ||
"on", "or", "that", "the", | ||
"this", "to", "was", "what", | ||
"when", "where", "who", "will", | ||
"with", "und", "the", "www" | ||
); | ||
|
||
private FullSearchQueryParser() { | ||
} | ||
|
||
public static String parse(String query) { | ||
if (!StringUtils.hasText(query)) { | ||
throw new InvalidInputException("Full text query parameter has @NotNull @NotBlank requirement"); | ||
} | ||
String parsedQuery = Stream.of(query.split("[ @.,+*]")) | ||
.filter(part -> !(part.isEmpty() || stopWords.contains(part.toLowerCase()))) | ||
.map(part -> "+" + part) | ||
.collect(Collectors.joining(" ")); | ||
return parsedQuery + "*"; | ||
} | ||
} |
This file contains 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 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 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 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
23 changes: 23 additions & 0 deletions
23
server/src/test/java/access/api/FullSearchQueryParserTest.java
This file contains 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,23 @@ | ||
package access.api; | ||
|
||
import access.exception.InvalidInputException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class FullSearchQueryParserTest { | ||
|
||
@Test | ||
void parse() { | ||
String parsed = FullSearchQueryParser.parse("This *is+ +a ** test for + the [email protected] *query*"); | ||
assertEquals("+test +john +doe +example +query*", parsed); | ||
} | ||
|
||
@Test | ||
void parseEmpty() { | ||
assertThrows(InvalidInputException.class, () -> FullSearchQueryParser.parse(null)); | ||
assertThrows(InvalidInputException.class, () -> FullSearchQueryParser.parse("")); | ||
assertThrows(InvalidInputException.class, () -> FullSearchQueryParser.parse(" ")); | ||
} | ||
|
||
} |
This file contains 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 |
---|---|---|
|
@@ -295,6 +295,22 @@ void search() throws Exception { | |
assertEquals(1, users.size()); | ||
} | ||
|
||
@Test | ||
void searchWithAtSign() throws Exception { | ||
AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", SUPER_SUB); | ||
|
||
List<User> users = given() | ||
.when() | ||
.filter(accessCookieFilter.cookieFilter()) | ||
.accept(ContentType.JSON) | ||
.contentType(ContentType.JSON) | ||
.queryParam("query", "[email protected]") | ||
.get("/api/v1/users/search") | ||
.as(new TypeRef<>() { | ||
}); | ||
assertEquals(1, users.size()); | ||
} | ||
|
||
@Test | ||
void searchPaginated() throws Exception { | ||
AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", SUPER_SUB); | ||
|
This file contains 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