Permalink
Browse files

Do not show unpublished pages in search result

Due to a bug in Hibernate Search, unpublished pages may be shown if they are the only search result. Now an additional check removes all unpublished or restricted pages from the search result.
  • Loading branch information...
shred committed Jan 5, 2019
1 parent 09d1070 commit ae66a1e7c09b1e593e893d52c75a4bb6aa0c1e73
@@ -19,6 +19,9 @@
*/
package org.shredzone.cilla.core.repository.impl;

import static java.util.stream.Collectors.toList;

import java.util.Collections;
import java.util.List;
import java.util.Locale;

@@ -69,20 +72,35 @@ public Query parseQuery(String query, Locale locale) throws ParseException{

@Override
public int count(Query query, Criteria crit) {
assertCriteriaEntity(crit);

FullTextSession fullTextSession = getFullTextSession();
FullTextQuery fq = fullTextSession.createFullTextQuery(query, Page.class);
fq.setCriteriaQuery(crit != null ? crit : pageDao.criteria());

// This is the only way to reliable get a result counter if restricting
// criteria apply.
return fq.list().size();
return (int) search(query, crit).stream()
.filter(SearchDaoHibImpl::isValidResult)
.count();
}

@SuppressWarnings("unchecked")
@Override
public List<Page> fetch(Query query, Criteria crit) {
return Collections.unmodifiableList(search(query, crit).stream()
.filter(SearchDaoHibImpl::isValidResult)
.collect(toList()));
}

/**
* Performs the search.
* <p>
* Due to a bug in Hibernate Search, the resulting list may contain unpublished pages
* if only one hit was found. It is recommended to check the resulting list and filter
* all unpublished pages, and other pages that are not supposed to be found.
*
* @param query
* {@link Query} to perform
* @param crit
* {@link Criteria} containing search parameters
* @return {@link List} of {@link Page} found
*/
@SuppressWarnings("unchecked")
private List<Page> search(Query query, Criteria crit) {
assertCriteriaEntity(crit);

FullTextSession fullTextSession = getFullTextSession();
@@ -92,6 +110,17 @@ public int count(Query query, Criteria crit) {
return fq.list();
}

/**
* Checks if the given {@link Page} is allowed to be seen in a search result.
*
* @param page
* {@link Page} to check
* @return {@code true}: valid search result, {@code false}: do not show as result
*/
private static boolean isValidResult(Page page) {
return page.isPublished() && !page.isRestricted();
}

/**
* Asserts that the given {@link Criteria} is bound to the correct entity.
*

0 comments on commit ae66a1e

Please sign in to comment.