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

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

Introduction

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

Prototype

public Serializable getAttribute(String name) 

Source Link

Usage

From source file:com.liferay.journal.search.JournalArticleIndexer.java

License:Open Source License

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

    Long classNameId = (Long) searchContext.getAttribute(Field.CLASS_NAME_ID);

    if ((classNameId != null) && (classNameId != 0)) {
        contextBooleanFilter.addRequiredTerm(Field.CLASS_NAME_ID, classNameId.toString());
    }//  www  . jav a2  s .  c o m

    addStatus(contextBooleanFilter, searchContext);

    addSearchClassTypeIds(contextBooleanFilter, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

        QueryFilter queryFilter = _ddmIndexer.createFieldValueQueryFilter(ddmStructureFieldName,
                ddmStructureFieldValue, searchContext.getLocale());

        contextBooleanFilter.add(queryFilter, BooleanClauseOccur.MUST);
    }

    String ddmStructureKey = (String) searchContext.getAttribute("ddmStructureKey");

    if (Validator.isNotNull(ddmStructureKey)) {
        contextBooleanFilter.addRequiredTerm("ddmStructureKey", ddmStructureKey);
    }

    String ddmTemplateKey = (String) searchContext.getAttribute("ddmTemplateKey");

    if (Validator.isNotNull(ddmTemplateKey)) {
        contextBooleanFilter.addRequiredTerm("ddmTemplateKey", ddmTemplateKey);
    }

    boolean head = GetterUtil.getBoolean(searchContext.getAttribute("head"), Boolean.TRUE);
    boolean relatedClassName = GetterUtil.getBoolean(searchContext.getAttribute("relatedClassName"));
    boolean showNonindexable = GetterUtil.getBoolean(searchContext.getAttribute("showNonindexable"));

    if (head && !relatedClassName && !showNonindexable) {
        contextBooleanFilter.addRequiredTerm("head", Boolean.TRUE);
    }

    if (!relatedClassName && showNonindexable) {
        contextBooleanFilter.addRequiredTerm("headListable", Boolean.TRUE);
    }
}

From source file:com.liferay.journal.search.JournalArticleIndexer.java

License:Open Source License

@Override
public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
        SearchContext searchContext) throws Exception {

    addSearchTerm(searchQuery, searchContext, Field.ARTICLE_ID, false);
    addSearchTerm(searchQuery, searchContext, Field.CLASS_PK, false);
    addSearchLocalizedTerm(searchQuery, searchContext, Field.CONTENT, false);
    addSearchLocalizedTerm(searchQuery, searchContext, Field.DESCRIPTION, false);
    addSearchTerm(searchQuery, searchContext, Field.ENTRY_CLASS_PK, false);
    addSearchLocalizedTerm(searchQuery, searchContext, Field.TITLE, false);
    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);

    LinkedHashMap<String, Object> params = (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

    if (params != null) {
        String expandoAttributes = (String) params.get("expandoAttributes");

        if (Validator.isNotNull(expandoAttributes)) {
            addSearchExpando(searchQuery, searchContext, expandoAttributes);
        }/*from w w w . ja  va  2 s . co m*/
    }
}

From source file:com.liferay.journal.search.JournalArticleIndexer.java

License:Open Source License

@Override
protected Map<String, Query> addSearchLocalizedTerm(BooleanQuery searchQuery, SearchContext searchContext,
        String field, boolean like) throws Exception {

    if (Validator.isNull(field)) {
        return Collections.emptyMap();
    }/*from w ww. j  a  v a 2s.  c om*/

    String value = String.valueOf(searchContext.getAttribute(field));

    if (Validator.isNull(value)) {
        value = searchContext.getKeywords();
    }

    if (Validator.isNull(value)) {
        return Collections.emptyMap();
    }

    String localizedField = DocumentImpl.getLocalizedName(searchContext.getLocale(), field);

    Map<String, Query> queries = new HashMap<>();

    if (Validator.isNull(searchContext.getKeywords())) {
        BooleanQuery localizedQuery = new BooleanQueryImpl();

        Query query = localizedQuery.addTerm(field, value, like);

        queries.put(field, query);

        Query localizedFieldQuery = localizedQuery.addTerm(localizedField, value, like);

        queries.put(field, localizedFieldQuery);

        BooleanClauseOccur booleanClauseOccur = BooleanClauseOccur.SHOULD;

        if (searchContext.isAndSearch()) {
            booleanClauseOccur = BooleanClauseOccur.MUST;
        }

        searchQuery.add(localizedQuery, booleanClauseOccur);
    } else {
        Query query = searchQuery.addTerm(localizedField, value, like);

        queries.put(field, query);
    }

    return queries;
}

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 va2s.c  om

    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.message.boards.internal.search.MBThreadIndexer.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);

    long endDate = GetterUtil.getLong(searchContext.getAttribute("endDate"));
    long startDate = GetterUtil.getLong(searchContext.getAttribute("startDate"));

    if ((endDate > 0) && (startDate > 0)) {
        contextBooleanFilter.addRangeTerm("lastPostDate", startDate, endDate);
    }//from   w w  w .  java  2  s.co m

    long participantUserId = GetterUtil.getLong(searchContext.getAttribute("participantUserId"));

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

From source file:com.liferay.portlet.documentlibrary.util.DLFileEntryIndexer.java

License:Open Source License

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

    addStatus(contextQuery, searchContext);

    if (searchContext.isIncludeAttachments()) {
        addRelatedClassNames(contextQuery, searchContext);
    }/* w  w w .j  a  v  a 2  s. c om*/

    contextQuery.addRequiredTerm(Field.HIDDEN, searchContext.isIncludeAttachments());

    addSearchClassTypeIds(contextQuery, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

        String[] ddmStructureFieldNameParts = StringUtil.split(ddmStructureFieldName, StringPool.SLASH);

        DDMStructure structure = DDMStructureLocalServiceUtil
                .getStructure(GetterUtil.getLong(ddmStructureFieldNameParts[1]));

        String fieldName = StringUtil.replaceLast(ddmStructureFieldNameParts[2],
                StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(searchContext.getLocale())),
                StringPool.BLANK);

        try {
            ddmStructureFieldValue = DDMUtil.getIndexedFieldValue(ddmStructureFieldValue,
                    structure.getFieldType(fieldName));
        } catch (StructureFieldException sfe) {
        }

        contextQuery.addRequiredTerm(ddmStructureFieldName,
                StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
    }

    String[] mimeTypes = (String[]) searchContext.getAttribute("mimeTypes");

    if (ArrayUtil.isNotEmpty(mimeTypes)) {
        BooleanQuery mimeTypesQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (String mimeType : mimeTypes) {
            mimeTypesQuery.addTerm("mimeType",
                    StringUtil.replace(mimeType, CharPool.FORWARD_SLASH, CharPool.UNDERLINE));
        }

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

From source file:com.liferay.portlet.documentlibrary.util.DLFileEntryIndexer.java

License:Open Source License

@Override
public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext) throws Exception {

    Group group = GroupLocalServiceUtil.getCompanyGroup(searchContext.getCompanyId());

    DDMStructure tikaRawMetadataStructure = DDMStructureLocalServiceUtil.fetchStructure(group.getGroupId(),
            PortalUtil.getClassNameId(RawMetadataProcessor.class), "TikaRawMetadata");

    if (tikaRawMetadataStructure != null) {
        addSearchDDMStruture(searchQuery, searchContext, tikaRawMetadataStructure);
    }/* w w  w .j a va2s  .  c om*/

    String keywords = searchContext.getKeywords();

    if (Validator.isNull(keywords)) {
        addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, false);
        addSearchTerm(searchQuery, searchContext, Field.TITLE, false);
        addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);
    }

    addSearchTerm(searchQuery, searchContext, "ddmContent", false);
    addSearchTerm(searchQuery, searchContext, "extension", false);
    addSearchTerm(searchQuery, searchContext, "fileEntryTypeId", false);
    addSearchTerm(searchQuery, searchContext, "path", false);

    LinkedHashMap<String, Object> params = (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

    if (params != null) {
        String expandoAttributes = (String) params.get("expandoAttributes");

        if (Validator.isNotNull(expandoAttributes)) {
            addSearchExpando(searchQuery, searchContext, expandoAttributes);
        }
    }
}

From source file:com.liferay.portlet.documentlibrary.util.DLIndexer.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_APPROVED);

    if (status != WorkflowConstants.STATUS_ANY) {
        contextQuery.addRequiredTerm(Field.STATUS, status);
    }//from   ww  w  .  j a v a2 s  .com

    long[] folderIds = searchContext.getFolderIds();

    if ((folderIds != null) && (folderIds.length > 0)) {
        if (folderIds[0] == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
            return;
        }

        BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (long folderId : folderIds) {
            try {
                DLFolderServiceUtil.getFolder(folderId);
            } catch (Exception e) {
                continue;
            }

            folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
        }

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

From source file:com.liferay.portlet.documentlibrary.util.DLIndexer.java

License:Open Source License

@Override
public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext) throws Exception {

    Set<DDMStructure> ddmStructuresSet = new TreeSet<DDMStructure>();

    long[] groupIds = searchContext.getGroupIds();

    if ((groupIds != null) && (groupIds.length > 0)) {
        List<DLFileEntryType> dlFileEntryTypes = DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(groupIds);

        for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
            ddmStructuresSet.addAll(dlFileEntryType.getDDMStructures());
        }/*from  w w  w.  jav  a 2  s .  co m*/
    }

    Group group = GroupLocalServiceUtil.getCompanyGroup(searchContext.getCompanyId());

    DDMStructure tikaRawMetadataStructure = DDMStructureLocalServiceUtil.fetchStructure(group.getGroupId(),
            "TikaRawMetadata");

    if (tikaRawMetadataStructure != null) {
        ddmStructuresSet.add(tikaRawMetadataStructure);
    }

    for (DDMStructure ddmStructure : ddmStructuresSet) {
        addSearchDDMStruture(searchQuery, searchContext, ddmStructure);
    }

    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);

    addSearchTerm(searchQuery, searchContext, "extension", false);
    addSearchTerm(searchQuery, searchContext, "fileEntryTypeId", false);
    addSearchTerm(searchQuery, searchContext, "path", false);

    LinkedHashMap<String, Object> params = (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

    if (params != null) {
        String expandoAttributes = (String) params.get("expandoAttributes");

        if (Validator.isNotNull(expandoAttributes)) {
            addSearchExpando(searchQuery, searchContext, expandoAttributes);
        }
    }
}

From source file:com.liferay.portlet.journal.util.JournalArticleIndexer.java

License:Open Source License

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

    Long classNameId = (Long) searchContext.getAttribute(Field.CLASS_NAME_ID);

    if ((classNameId != null) && (classNameId.longValue() != 0)) {
        contextQuery.addRequiredTerm("classNameId", classNameId.toString());
    }//w ww . ja  v  a2 s  . c  o m

    addStatus(contextQuery, searchContext);

    addSearchClassTypeIds(contextQuery, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

        String[] ddmStructureFieldNameParts = StringUtil.split(ddmStructureFieldName, StringPool.SLASH);

        DDMStructure structure = DDMStructureLocalServiceUtil
                .getStructure(GetterUtil.getLong(ddmStructureFieldNameParts[1]));

        String fieldName = StringUtil.replaceLast(ddmStructureFieldNameParts[2],
                StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(searchContext.getLocale())),
                StringPool.BLANK);

        try {
            ddmStructureFieldValue = DDMUtil.getIndexedFieldValue(ddmStructureFieldValue,
                    structure.getFieldType(fieldName));
        } catch (StructureFieldException sfe) {
        }

        contextQuery.addRequiredTerm(ddmStructureFieldName,
                StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
    }

    String articleType = (String) searchContext.getAttribute("articleType");

    if (Validator.isNotNull(articleType)) {
        contextQuery.addRequiredTerm(Field.TYPE, articleType);
    }

    String ddmStructureKey = (String) searchContext.getAttribute("ddmStructureKey");

    if (Validator.isNotNull(ddmStructureKey)) {
        contextQuery.addRequiredTerm("ddmStructureKey", ddmStructureKey);
    }

    String ddmTemplateKey = (String) searchContext.getAttribute("ddmTemplateKey");

    if (Validator.isNotNull(ddmTemplateKey)) {
        contextQuery.addRequiredTerm("ddmTemplateKey", ddmTemplateKey);
    }
}