Example usage for com.liferay.portal.kernel.search SearchContext getCategoryIds

List of usage examples for com.liferay.portal.kernel.search SearchContext getCategoryIds

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.search SearchContext getCategoryIds.

Prototype

public long[] getCategoryIds() 

Source Link

Usage

From source file:com.liferay.message.boards.internal.search.MBMessageIndexer.java

License:Open Source License

@Override
public void postProcessContextBooleanFilter(BooleanFilter contextBooleanFilter, SearchContext searchContext)
        throws Exception {

    addStatus(contextBooleanFilter, searchContext);

    boolean discussion = GetterUtil.getBoolean(searchContext.getAttribute("discussion"));

    contextBooleanFilter.addRequiredTerm("discussion", discussion);

    if (searchContext.isIncludeDiscussions()) {
        addRelatedClassNames(contextBooleanFilter, searchContext);
    }//from w  w w . j a  v a  2  s .  co m

    String classNameId = GetterUtil.getString(searchContext.getAttribute(Field.CLASS_NAME_ID));

    if (Validator.isNotNull(classNameId)) {
        contextBooleanFilter.addRequiredTerm(Field.CLASS_NAME_ID, classNameId);
    }

    long threadId = GetterUtil.getLong((String) searchContext.getAttribute("threadId"));

    if (threadId > 0) {
        contextBooleanFilter.addRequiredTerm("threadId", threadId);
    }

    long[] categoryIds = searchContext.getCategoryIds();

    if ((categoryIds != null) && (categoryIds.length > 0)
            && (categoryIds[0] != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {

        TermsFilter categoriesTermsFilter = new TermsFilter(Field.CATEGORY_ID);

        for (long categoryId : categoryIds) {
            try {
                mbCategoryService.getCategory(categoryId);
            } catch (PortalException pe) {
                if (_log.isDebugEnabled()) {
                    _log.debug("Unable to get message boards category " + categoryId, pe);
                }

                continue;
            }

            categoriesTermsFilter.addValue(String.valueOf(categoryId));
        }

        if (!categoriesTermsFilter.isEmpty()) {
            contextBooleanFilter.add(categoriesTermsFilter, BooleanClauseOccur.MUST);
        }
    }
}

From source file:com.liferay.portlet.messageboards.util.MBIndexer.java

License:Open Source License

@Override
public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

    int status = GetterUtil.getInteger(searchContext.getAttribute(Field.STATUS), WorkflowConstants.STATUS_ANY);

    if (status != WorkflowConstants.STATUS_ANY) {
        contextQuery.addRequiredTerm(Field.STATUS, status);
    }/*from   ww  w  .  j a  v a  2  s  .c  o  m*/

    boolean discussion = GetterUtil.getBoolean(searchContext.getAttribute("discussion"), false);

    contextQuery.addRequiredTerm("discussion", discussion);

    long threadId = GetterUtil.getLong((String) searchContext.getAttribute("threadId"));

    if (threadId > 0) {
        contextQuery.addRequiredTerm("threadId", threadId);
    }

    long[] categoryIds = searchContext.getCategoryIds();

    if ((categoryIds != null) && (categoryIds.length > 0)) {
        if (categoryIds[0] == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

            return;
        }

        BooleanQuery categoriesQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (long categoryId : categoryIds) {
            try {
                MBCategoryServiceUtil.getCategory(categoryId);
            } catch (Exception e) {
                continue;
            }

            categoriesQuery.addTerm(Field.CATEGORY_ID, categoryId);
        }

        contextQuery.add(categoriesQuery, BooleanClauseOccur.MUST);
    }
}