Skip to content

Instantly share code, notes, and snippets.

@oharsta
Created December 11, 2024 11:38
Show Gist options
  • Save oharsta/ec5a9e9a4f4f78c5978b867fea72c023 to your computer and use it in GitHub Desktop.
Save oharsta/ec5a9e9a4f4f78c5978b867fea72c023 to your computer and use it in GitHub Desktop.
MySQL full text indexes autocomplete query parser
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"
);
public static String parse(String query) {
if (!StringUtils.hasText(query)) {
return "";
}
String parsedQuery = Stream.of(query.split("[ @.,+*]"))
.filter(part -> !(part.isEmpty() || stopWords.contains(part.toLowerCase())))
.map(part -> "+" + part)
.collect(Collectors.joining(" "));
return parsedQuery + "*";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment