title: How to combine Hibernate Search (Lucene) with paging and ACLs
body: <p>I am using Spring Security with ACLs to secure the documents in my application. On the other hand I use Hibernate Search (on top of lucene) to search for the documents. This search also support paging. (<em>Documents are only meta data of documents stored in a Database.</em>)</p>
  
  <pre><code>FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
  QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Document.class).get();
  Query query = queryBuilder.keyword().onFields(fieldNames.toArray(new String[0])).matching(searchQuery)
              .createQuery();
  
  FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Document.class);
  fullTextQuery.setFirstResult(pageable.getFirstItem());
  fullTextQuery.setMaxResults(pageable.getPageSize());
  </code></pre>
  
  <p>Now I have to combine the paging with the ACLs. The only idea I have at the moment, is to remove the paging form the FullTextQuery, read all search result documents, filter them by there ACLs and then do the paging by hand. But I don't like that solution, because it loads all the documents, instead of only the one for the page.</p>
  
  <p>Does anybody have a better idea?</p>