Last active
June 12, 2018 22:31
-
-
Save ilmassa/3484218123288d765ed63565e9d17b2f to your computer and use it in GitHub Desktop.
A simple liferay facet to add filter for a specific asset
This file contains 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
package biz.ilmassa.liferay.portal.kernel.search.facet; | |
import com.liferay.portal.kernel.json.JSONArray; | |
import com.liferay.portal.kernel.json.JSONObject; | |
import com.liferay.portal.kernel.log.Log; | |
import com.liferay.portal.kernel.log.LogFactoryUtil; | |
import com.liferay.portal.kernel.search.BooleanClause; | |
import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil; | |
import com.liferay.portal.kernel.search.BooleanClauseOccur; | |
import com.liferay.portal.kernel.search.BooleanQuery; | |
import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil; | |
import com.liferay.portal.kernel.search.Field; | |
import com.liferay.portal.kernel.search.SearchContext; | |
import com.liferay.portal.kernel.search.TermQuery; | |
import com.liferay.portal.kernel.search.TermQueryFactoryUtil; | |
import com.liferay.portal.kernel.search.facet.MultiValueFacet; | |
import com.liferay.portal.kernel.search.facet.config.FacetConfiguration; | |
import com.liferay.portal.kernel.search.facet.util.FacetValueValidator; | |
import com.liferay.portal.kernel.util.StringPool; | |
import com.liferay.portal.kernel.util.StringUtil; | |
import com.liferay.portal.kernel.util.Validator; | |
public class MultiValueFilterFacet extends MultiValueFacet { | |
public MultiValueFilterFacet(SearchContext searchContext) { | |
super(searchContext); | |
} | |
/* | |
* (non-Javadoc) | |
* @see com.liferay.portal.kernel.search.facet.MultiValueFacet#doGetFacetClause() | |
*/ | |
@Override | |
protected BooleanClause doGetFacetClause() { | |
// if not in static mode, become a standard multi-value facet | |
if(!isStatic()) | |
return super.doGetFacetClause(); | |
SearchContext searchContext = getSearchContext(); | |
FacetConfiguration facetConfiguration = getFacetConfiguration(); | |
JSONObject dataJSONObject = facetConfiguration.getData(); | |
// fetch configuration... | |
String fieldName = getFieldName(); | |
String applyTo = dataJSONObject.getString(PARAM_APPLY_TO, StringPool.BLANK); | |
String[] values = null; | |
if(dataJSONObject.has(PARAM_VALUES)){ | |
JSONArray valuesJSONArray = dataJSONObject.getJSONArray(PARAM_VALUES); | |
values = new String[valuesJSONArray.length()]; | |
for(int i = 0; i < valuesJSONArray.length(); i++) { | |
values[i] = valuesJSONArray.getString(i); | |
} | |
} | |
// check parameters... | |
if(Validator.isBlank(fieldName) || Validator.isBlank(applyTo) || values == null) | |
return null; | |
try { | |
/* | |
* Values filter | |
*/ | |
BooleanQuery valuesFilterQuery = BooleanQueryFactoryUtil.create(searchContext); | |
for(String val : values){ | |
FacetValueValidator facetValueValidator = getFacetValueValidator(); | |
if(searchContext.getUserId() <= 0 || facetValueValidator.check(searchContext, val)){ | |
TermQuery termQuery = TermQueryFactoryUtil.create(searchContext, getFieldName(), val); | |
valuesFilterQuery.add(termQuery, BooleanClauseOccur.SHOULD.getName()); | |
} | |
} | |
/* | |
* Add class constraint to query | |
* es: (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +(extension:doc extension:pdf extension:...) | |
*/ | |
BooleanQuery valuesFullQuery = BooleanQueryFactoryUtil.create(searchContext); | |
TermQuery classQuery = TermQueryFactoryUtil.create(searchContext, Field.ENTRY_CLASS_NAME, applyTo); | |
valuesFullQuery.add(classQuery, BooleanClauseOccur.MUST); | |
valuesFullQuery.add(valuesFilterQuery, BooleanClauseOccur.MUST); | |
/* | |
* Add other classes if need | |
* es: (entryClassName:xyz entryClassName:aaa (+entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry +(extension:doc extension:pdf extension:...)) | |
*/ | |
BooleanQuery mainFullQuery = BooleanQueryFactoryUtil.create(searchContext); | |
if(searchContext.getEntryClassNames() != null && searchContext.getEntryClassNames().length > 0){ | |
for(String c : searchContext.getEntryClassNames()){ | |
if(!c.equals(applyTo)){ | |
TermQuery otherClassQuery = TermQueryFactoryUtil.create(searchContext, Field.ENTRY_CLASS_NAME, c); | |
mainFullQuery.add(otherClassQuery, BooleanClauseOccur.SHOULD); | |
} else { | |
mainFullQuery.add(valuesFullQuery, BooleanClauseOccur.SHOULD); | |
} | |
} | |
} | |
/* | |
* Return to the appropriate query based on the input | |
*/ | |
if(mainFullQuery.hasClauses()){ | |
return BooleanClauseFactoryUtil.create(searchContext, mainFullQuery, BooleanClauseOccur.MUST.getName()); | |
} | |
if(valuesFullQuery.hasClauses()){ | |
return BooleanClauseFactoryUtil.create(searchContext, valuesFullQuery, BooleanClauseOccur.MUST.getName()); | |
} | |
} catch(Exception e){ | |
_log.error(e); | |
} | |
return null; | |
} | |
/** | |
* @param applyTo | |
* classname to apply filter | |
*/ | |
public void setApplyTo(String applyTo){ | |
FacetConfiguration facetConfiguration = getFacetConfiguration(); | |
JSONObject dataJSONObject = facetConfiguration.getData(); | |
dataJSONObject.put(PARAM_APPLY_TO, StringUtil.trim(applyTo)); | |
} | |
private static final String PARAM_APPLY_TO = "applyTo"; | |
private final static String PARAM_VALUES = "values"; | |
private static final Log _log = LogFactoryUtil.getLog(MultiValueFilterFacet.class); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment