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

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

Introduction

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

Prototype

public void setSorts(Sort... sorts) 

Source Link

Usage

From source file:ch.inofix.contact.service.impl.ContactLocalServiceImpl.java

License:Open Source License

protected SearchContext buildSearchContext(long userId, long groupId, long ownerUserId, String company,
        String fullName, int status, LinkedHashMap<String, Object> params, boolean andSearch, int start,
        int end, Sort sort) throws PortalException {

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.STATUS, status);

    if (Validator.isNotNull(company)) {
        searchContext.setAttribute("company", company);
    }//from  w w  w  . j a v a  2  s .  c o  m

    if (Validator.isNotNull(fullName)) {
        searchContext.setAttribute("fullName", fullName);
    }

    searchContext.setAttribute("paginationType", "more");

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    searchContext.setCompanyId(group.getCompanyId());

    if (ownerUserId > 0) {
        searchContext.setOwnerUserId(ownerUserId);
    }

    searchContext.setEnd(end);
    if (groupId > 0) {
        searchContext.setGroupIds(new long[] { groupId });
    }
    searchContext.setSorts(sort);
    searchContext.setStart(start);
    searchContext.setUserId(userId);

    searchContext.setAndSearch(andSearch);

    if (params != null) {

        String keywords = (String) params.remove("keywords");

        if (Validator.isNotNull(keywords)) {
            searchContext.setKeywords(keywords);
        }
    }

    QueryConfig queryConfig = new QueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    searchContext.setQueryConfig(queryConfig);

    if (sort != null) {
        searchContext.setSorts(sort);
    }

    searchContext.setStart(start);

    return searchContext;
}

From source file:ch.inofix.referencemanager.service.impl.BibliographyServiceImpl.java

License:Open Source License

/**
 * @param userId/*  w w  w.jav a  2 s  .  c o m*/
 *            the userId of the current user
 * @param groupId
 *            the scopeGroupId of the bibliography. 0 means: any scope.
 * @param ownerUserId
 *            the userId of the bibliography owner. -1 means: ignore
 *            ownerUserId parameter.
 * @param keywords
 * @param start
 * @param end
 * @param sort
 * @return the hits for the given parameters
 * @since 1.0.0
 * @throws PortalException
 */
public Hits search(long userId, long groupId, long ownerUserId, String keywords, int start, int end, Sort sort)
        throws PortalException {

    if (sort == null) {
        sort = new Sort(Field.MODIFIED_DATE, true);
    }

    Indexer<Bibliography> indexer = IndexerRegistryUtil.getIndexer(Bibliography.class.getName());

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.STATUS, WorkflowConstants.STATUS_ANY);

    searchContext.setAttribute("paginationType", "more");

    User user = UserLocalServiceUtil.getUser(userId);

    searchContext.setCompanyId(user.getCompanyId());

    searchContext.setEnd(end);

    if (groupId > 0) {
        searchContext.setGroupIds(new long[] { groupId });
    }
    searchContext.setSorts(sort);
    searchContext.setStart(start);
    searchContext.setUserId(userId);
    searchContext.setOwnerUserId(ownerUserId);

    return indexer.search(searchContext);

}

From source file:ch.inofix.referencemanager.service.impl.ReferenceLocalServiceImpl.java

License:Open Source License

protected SearchContext buildSearchContext(long userId, long groupId, long bibliographyId, String author,
        String title, String year, int status, LinkedHashMap<String, Object> params, boolean andSearch,
        int start, int end, Sort sort) throws PortalException {

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.STATUS, status);

    if (Validator.isNotNull(author)) {
        searchContext.setAttribute("author", author);
    }//  w w  w . j  av  a 2  s. c om

    if (bibliographyId > 0) {
        searchContext.setAttribute("bibliographyId", bibliographyId);
    }

    if (Validator.isNotNull(title)) {
        searchContext.setAttribute("title", title);
    }

    if (Validator.isNotNull(year)) {
        searchContext.setAttribute("year", year);
    }

    searchContext.setAttribute("paginationType", "more");

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    searchContext.setCompanyId(group.getCompanyId());

    searchContext.setEnd(end);
    if (groupId > 0) {
        searchContext.setGroupIds(new long[] { groupId });
    }
    searchContext.setSorts(sort);
    searchContext.setStart(start);
    searchContext.setUserId(userId);

    searchContext.setAndSearch(andSearch);

    if (params != null) {

        String keywords = (String) params.remove("keywords");

        if (Validator.isNotNull(keywords)) {
            searchContext.setKeywords(keywords);
        }
    }

    QueryConfig queryConfig = new QueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    searchContext.setQueryConfig(queryConfig);

    if (sort != null) {
        searchContext.setSorts(sort);
    }

    searchContext.setStart(start);

    return searchContext;
}

From source file:ch.inofix.timetracker.service.impl.TaskRecordLocalServiceImpl.java

License:Open Source License

@Override
public Hits search(long userId, long groupId, String keywords, int start, int end, Sort sort)
        throws PortalException {

    if (sort == null) {
        sort = new Sort(Field.MODIFIED_DATE, true);
    }//  www  .j  av  a2  s. c o  m

    Indexer<TaskRecord> indexer = IndexerRegistryUtil.getIndexer(TaskRecord.class.getName());

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.STATUS, WorkflowConstants.STATUS_ANY);

    searchContext.setAttribute("paginationType", "more");

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    searchContext.setCompanyId(group.getCompanyId());

    searchContext.setEnd(end);
    if (groupId > 0) {
        searchContext.setGroupIds(new long[] { groupId });
    }
    searchContext.setSorts(sort);
    searchContext.setStart(start);
    searchContext.setUserId(userId);

    searchContext.setKeywords(keywords);

    return indexer.search(searchContext);

}

From source file:com.library.slayer.service.impl.LMSBookLocalServiceImpl.java

License:Open Source License

public List<LMSBook> advanceSearchAll(long companyId, long groupId, String title, String author, String desc,
        String type) {/*from   w ww. j  a  va2 s  .c  om*/

    SearchContext searchContext = new SearchContext();
    searchContext.setCompanyId(companyId);
    searchContext.setEntryClassNames(CLASS_NAMES);

    BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(searchContext);
    contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
    contextQuery.addRequiredTerm(Field.PORTLET_ID,
            Long.toString(ClassNameLocalServiceUtil.getClassNameId(LMSBook.class)));

    BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
    fullQuery.setQueryConfig(searchContext.getQueryConfig());
    try {
        fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);
    Hits hits = null;

    if (Validator.isNotNull(title)) {
        Sort sort = new Sort(Field.TITLE, true);
        searchContext.setSorts(new Sort[] { sort });

        fullQuery = createQuery(Field.TITLE, title, searchContext, fullQuery);

    }
    if (Validator.isNotNull(author)) {
        fullQuery = createQuery(Field.NAME, author, searchContext, fullQuery);
    }
    if (Validator.isNotNull(desc)) {
        fullQuery = createQuery(Field.DESCRIPTION, desc, searchContext, fullQuery);
    }

    if (searchQuery.clauses().size() > 0) {
        try {
            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        hits = SearchEngineUtil.search(searchContext, fullQuery);
    } catch (SearchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<LMSBook> books = new ArrayList<LMSBook>();

    if (hits != null && hits.getLength() > 0) {
        for (Document document : hits.getDocs()) {

            long bookId = Long.parseLong(document.get(Field.ENTRY_CLASS_PK));
            try {
                LMSBook book = LMSBookLocalServiceUtil.getLMSBook(bookId);
                books.add(book);
            } catch (PortalException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return books;
}

From source file:com.liferay.alloy.mvc.BaseAlloyControllerImpl.java

License:Open Source License

protected AlloySearchResult search(Indexer indexer, AlloyServiceInvoker alloyServiceInvoker,
        HttpServletRequest request, PortletRequest portletRequest,
        SearchContainer<? extends BaseModel<?>> searchContainer, Map<String, Serializable> attributes,
        String keywords, Sort[] sorts) throws Exception {

    if (indexer == null) {
        throw new Exception("No indexer found for " + controllerPath);
    }//from   ww w .j  a v  a2 s  .  c  om

    AlloySearchResult alloySearchResult = new AlloySearchResult();

    alloySearchResult.setAlloyServiceInvoker(alloyServiceInvoker);

    if (searchContainer == null) {
        searchContainer = new SearchContainer<BaseModel<?>>(portletRequest, portletURL, null, null);
    }

    SearchContext searchContext = SearchContextFactory.getInstance(request);

    boolean andOperator = ParamUtil.getBoolean(request, "andOperator");

    searchContext.setAndSearch(andOperator);

    if ((attributes != null) && !attributes.isEmpty()) {
        searchContext.setAttributes(attributes);
    }

    searchContext.setEnd(searchContainer.getEnd());

    Class<?> indexerClass = Class.forName(indexer.getClassNames()[0]);

    if (!GroupedModel.class.isAssignableFrom(indexerClass)) {
        searchContext.setGroupIds(null);
    } else if (searchContext.getAttribute(Field.GROUP_ID) != null) {
        long groupId = GetterUtil.getLong(searchContext.getAttribute(Field.GROUP_ID));

        searchContext.setGroupIds(new long[] { groupId });
    }

    if (Validator.isNotNull(keywords)) {
        searchContext.setKeywords(keywords);
    }

    if (ArrayUtil.isNotEmpty(sorts)) {
        searchContext.setSorts(sorts);
    }

    searchContext.setStart(searchContainer.getStart());

    Hits hits = indexer.search(searchContext);

    alloySearchResult.setHits(hits);

    alloySearchResult.setPortletURL(portletURL, searchContext.getAttributes());

    alloySearchResult.afterPropertiesSet();

    return alloySearchResult;
}

From source file:com.liferay.asset.internal.util.AssetHelperImpl.java

License:Open Source License

private AssetSearcher _getAssetSearcher(SearchContext searchContext, AssetEntryQuery assetEntryQuery, int start,
        int end) throws Exception {

    Indexer<?> searcher = AssetSearcher.getInstance();

    AssetSearcher assetSearcher = (AssetSearcher) searcher;

    assetSearcher.setAssetEntryQuery(assetEntryQuery);

    Layout layout = assetEntryQuery.getLayout();

    if (layout != null) {
        searchContext.setAttribute(Field.LAYOUT_UUID, layout.getUuid());
    }// w w w.j a va2  s.  c o m

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

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

        searchContext.setAttribute("ddmStructureFieldName", ddmStructureFieldName);
        searchContext.setAttribute("ddmStructureFieldValue", ddmStructureFieldValue);
    }

    String paginationType = GetterUtil.getString(assetEntryQuery.getPaginationType(), "more");

    if (!paginationType.equals("none") && !paginationType.equals("simple")) {

        searchContext.setAttribute("paginationType", paginationType);
    }

    searchContext.setClassTypeIds(assetEntryQuery.getClassTypeIds());
    searchContext.setEnd(end);
    searchContext.setGroupIds(assetEntryQuery.getGroupIds());

    if (Validator.isNull(assetEntryQuery.getKeywords())) {
        QueryConfig queryConfig = searchContext.getQueryConfig();

        queryConfig.setScoreEnabled(false);
    } else {
        searchContext.setLike(true);
    }

    searchContext.setSorts(_getSorts(assetEntryQuery, searchContext.getLocale()));
    searchContext.setStart(start);

    return assetSearcher;
}

From source file:com.liferay.bookmarks.service.impl.BookmarksEntryLocalServiceImpl.java

License:Open Source License

@Override
public Hits search(long groupId, long userId, long creatorUserId, int status, int start, int end)
        throws PortalException {

    Indexer<BookmarksEntry> indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class.getName());

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.STATUS, status);

    if (creatorUserId > 0) {
        searchContext.setAttribute(Field.USER_ID, String.valueOf(creatorUserId));
    }//  ww  w .  ja va  2 s . c om

    searchContext.setAttribute("paginationType", "none");

    Group group = groupLocalService.getGroup(groupId);

    searchContext.setCompanyId(group.getCompanyId());

    searchContext.setEnd(end);
    searchContext.setGroupIds(new long[] { groupId });
    searchContext.setSorts(new Sort(Field.MODIFIED_DATE, true));
    searchContext.setStart(start);
    searchContext.setUserId(userId);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    return indexer.search(searchContext);
}

From source file:com.liferay.experts.service.impl.QuestionLocalServiceImpl.java

License:Open Source License

public Hits search(long companyId, long groupId, String keywords, LinkedHashMap<String, Object> params,
        int start, int end, Sort sort) throws SystemException {

    try {//from  w  w w.  ja  v a 2s  . co  m
        SearchContext searchContext = new SearchContext();

        searchContext.setCompanyId(companyId);
        searchContext.setEnd(end);
        searchContext.setGroupIds(new long[] { groupId });

        if (params != null) {
            if (Validator.isNull(keywords)) {
                keywords = (String) params.get("keywords");
            }

            params.remove("keywords");
        }

        if (Validator.isNotNull(keywords)) {
            searchContext.setKeywords(keywords);
        }

        QueryConfig queryConfig = new QueryConfig();

        queryConfig.setHighlightEnabled(false);

        searchContext.setQueryConfig(queryConfig);

        if (sort != null) {
            searchContext.setSorts(new Sort[] { sort });
        }

        searchContext.setStart(start);

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(Question.class);

        return indexer.search(searchContext);
    } catch (Exception e) {
        throw new SystemException(e);
    }
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected SearchContext buildSearchContext(long companyId, long groupId, List<Long> folderIds, long classNameId,
        String articleId, String title, String description, String content, int status, String ddmStructureKey,
        String ddmTemplateKey, LinkedHashMap<String, Object> params, boolean andSearch, int start, int end,
        Sort sort) {/* w  w w  . j  ava 2 s . c o  m*/

    SearchContext searchContext = new SearchContext();

    searchContext.setAndSearch(andSearch);

    Map<String, Serializable> attributes = new HashMap<>();

    attributes.put(Field.ARTICLE_ID, articleId);
    attributes.put(Field.CLASS_NAME_ID, classNameId);
    attributes.put(Field.CONTENT, content);
    attributes.put(Field.DESCRIPTION, description);
    attributes.put(Field.STATUS, status);
    attributes.put(Field.TITLE, title);
    attributes.put("ddmStructureKey", ddmStructureKey);
    attributes.put("ddmTemplateKey", ddmTemplateKey);
    attributes.put("params", params);

    searchContext.setAttributes(attributes);

    searchContext.setCompanyId(companyId);
    searchContext.setEnd(end);
    searchContext.setFolderIds(folderIds);
    searchContext.setGroupIds(new long[] { groupId });

    if (params != null) {
        searchContext.setIncludeDiscussions(GetterUtil.getBoolean(params.get("includeDiscussions")));

        String keywords = (String) params.remove("keywords");

        if (Validator.isNotNull(keywords)) {
            searchContext.setKeywords(keywords);
        }
    }

    QueryConfig queryConfig = new QueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    searchContext.setQueryConfig(queryConfig);

    if (sort != null) {
        searchContext.setSorts(sort);
    }

    searchContext.setStart(start);

    return searchContext;
}