Created
          December 11, 2024 11:38 
        
      - 
      
- 
        Save oharsta/ec5a9e9a4f4f78c5978b867fea72c023 to your computer and use it in GitHub Desktop. 
    MySQL full text indexes autocomplete query parser
  
        
  
    
      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
    
  
  
    
  | 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