You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Spring Data JpaRepository在所有列中查找含字母e的单词

Solution: Search All Columns for Words Containing "e"

Got it, let's fix this so your endpoint checks every column in your Note entity and returns all unique words that include the letter "e". Here's how to do it step by step:

Step 1: Clarify your Note entity fields

First, list out all text-based fields in your Note entity (e.g., content, title, author, category—adjust this to match your actual entity!). We'll need to inspect each of these fields for words containing "e".

Step 2: Update your Repository

We can either use the built-in findAll() method to fetch full note records, or write a custom native query for better performance (more on that later). Keep your existing method if you still need it, and add what we need:

public interface NoteRepository extends JpaRepository<Note,Long> {
    // Keep your original method if required
    List<Note> findByContentContains(String content);
    
    // Option 1: Use findAll() to fetch all notes for full column inspection
    // Option 2: Custom native query (example for PostgreSQL)
    @Query(value = "SELECT DISTINCT unnest(string_to_array(concat_ws(' ', n.content, n.title, n.author), ' ')) " +
                   "FROM note n " +
                   "WHERE unnest(string_to_array(concat_ws(' ', n.content, n.title, n.author), ' ')) LIKE '%e%'",
           nativeQuery = true)
    List<String> findAllWordsContainingE();
}

Step 3: Modify your Controller method

Choose one of the two approaches below based on your needs:

Approach 1: Java Stream Processing (Flexible, Works with Any Database)

This method fetches all notes, then iterates over each field to extract and filter words:

@GetMapping("/notesletter")
public List<String> getLetters() {
    // Map to your actual Note fields
    Function<Note, String>[] textFields = new Function[] {
        Note::getContent,
        Note::getTitle,
        Note::getAuthor
    };

    return noteRepository.findAll()
            .stream()
            // Process each field of every note
            .flatMap(note -> Arrays.stream(textFields)
                    .map(field -> field.apply(note))
                    // Skip null/empty values
                    .filter(Objects::nonNull)
                    .filter(text -> !text.isBlank())
                    // Split text into words (adjust regex for separators like commas/periods)
                    .flatMap(text -> Arrays.stream(text.split("[\\s.,!?]+")))
                    // Filter words containing "e" (add .toLowerCase() for case-insensitive match)
                    .filter(word -> word.contains("e"))
            )
            // Remove duplicate words
            .distinct()
            .collect(Collectors.toList());
}

Approach 2: Native Database Query (Faster for Large Datasets)

If you have a lot of notes, use the custom repository query we wrote earlier to offload the work to the database:

@GetMapping("/notesletter")
public List<String> getLetters() {
    return noteRepository.findAllWordsContainingE();
}

Key Tips:

  • Case Insensitivity: To match both "E" and "e", modify the filter to word.toLowerCase().contains("e") (for Java stream) or change the SQL LIKE to ILIKE '%e%' (PostgreSQL-specific).
  • Word Separators: Adjust the split regex (e.g., [\\s.,!?]+) to match how words are separated in your text.
  • Performance: The native query approach is faster for large datasets because it avoids fetching full note records and processes data directly in the database.

内容的提问来源于stack exchange,提问作者Maksym Sereda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:18:25