title: How to implement simple full text search in JPA (Spring Data JPA)?
body: <p>i'm using JPA 2.1 (Hibernate 4 as impl) and Spring Data JPA 1.9.0. How do i implement full text search? </p>
  
  <p>My scenario is as follows. I have a <code>User</code> entity and on the UI a have a table which display's most of users properties and i want the user to give text box enter there a search term and search in all properties. </p>
  
  <p>I see 2 options to do this:</p>
  
  <ol>
  <li>Load all users users from DB and filter them in Java </li>
  <li>Write a JPQL query with many <code>ORs</code> and <code>LIKE % :searchString %</code></li>
  </ol>
  
  <p><strong>Option 1</strong> is not good for performance but quite nice to write.</p>
  
  <p><strong>Option 2</strong> is performant beacuse executed on DB side but cumbersome to write. </p>
  
  <p>Right now im suing option 1 because i need to translate boolean to <code>"yes"/"no"</code> and also have a profile enum where i want to search by it's field description and not by actual enum value. </p>
  
  <p>In the User entity i have a method which returns all fields i want to be searched seperated by spaces:</p>
  
  <pre><code>   public String getSearchString(){
        return StringUtils.join(
                Arrays.asList(
                        login, 
                        firstName, 
                        lastName, 
                        email, 
                        active ? "yes" : "no", 
                        profile.getDescription())
                , " ");
     }
  </code></pre>
  
  <p>The in a service i load all users from DB and filter by this search string:</p>
  
  <pre><code>   @Override
     public List&lt;User&gt; getUsers(final String searchText) {
        final List&lt;User&gt; users = getUsers();
        if(StringUtils.isBlank(searchText)){
           return users;
        }
        CollectionUtils.filter(users, new Predicate&lt;User&gt;() {
           @Override
           public boolean evaluate(User object) {
              return StringUtils.containsIgnoreCase(object.getSearchString(), searchText);
           }
        });
        return users;
     }
  </code></pre>
  
  <p>On the other side in JPQL i end up with queries like this, which i dont think is the nice'est and easiest way to implement this functionality. Also there is a problem with translatin boolean to "yes" and "no".</p>
  
  <pre><code>@Query("SELECT r FROM User r WHERE "
          + "r.firstname LIKE '%' || :searchString || '%' "
          + "OR r.lastname LIKE '%' || :searchString || '%' "
          + "OR r.login LIKE '%' || :searchString || '%' "
          + "OR r.profile.description LIKE '%' || :searchString || '%' "
          + "OR r.active LIKE '%' || :searchString || '%' "
          + "OR r.email LIKE '%' || :searchString || '%'")
  List&lt;User&gt; selectUsers(@Param("searchString")String searchString, Pageable page);
  </code></pre>
  
  <p><strong>Is there a better solution to this problem?</strong></p>