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

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

Introduction

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

Prototype

public void setAndSearch(boolean andSearch) 

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 www .j  ava2s .com*/

    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.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  a  v  a  2s.c  o m

    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: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  w w  w . j a  va2  s . com

    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.configuration.admin.web.internal.portlet.action.SearchMVCRenderCommand.java

License:Open Source License

@Override
public String render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException {

    Indexer indexer = _indexerRegistry.nullSafeGetIndexer(ConfigurationModel.class);

    SearchContext searchContext = new SearchContext();

    searchContext.setAndSearch(false);
    searchContext.setCompanyId(CompanyConstants.SYSTEM);
    searchContext.setLocale(renderRequest.getLocale());

    String keywords = renderRequest.getParameter("keywords");

    if (Validator.isNotNull(keywords)) {
        searchContext.setKeywords(keywords);
    }/*from  w  w  w .  ja v a2 s. c  o m*/

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(true);
    queryConfig.setLocale(renderRequest.getLocale());
    queryConfig.setScoreEnabled(true);

    try {
        Hits hits = indexer.search(searchContext);

        Document[] documents = hits.getDocs();

        Map<String, ConfigurationModel> configurationModels = _configurationModelRetriever
                .getConfigurationModels();

        List<ConfigurationModel> searchResults = new ArrayList<>(documents.length);

        for (Document document : documents) {
            String configurationModelId = document.get(FieldNames.CONFIGURATION_MODEL_ID);

            ConfigurationModel configurationModel = configurationModels.get(configurationModelId);

            if (configurationModel == null) {
                String configurationModelFactoryId = document.get(FieldNames.CONFIGURATION_MODEL_FACTORY_PID);

                configurationModel = configurationModels.get(configurationModelFactoryId);
            }

            if (configurationModel != null) {
                searchResults.add(configurationModel);
            }
        }

        ConfigurationModelIterator configurationModelIterator = new ConfigurationModelIterator(searchResults);

        renderRequest.setAttribute(ConfigurationAdminWebKeys.CONFIGURATION_MODEL_ITERATOR,
                configurationModelIterator);

        renderRequest.setAttribute(ConfigurationAdminWebKeys.RESOURCE_BUNDLE_LOADER_PROVIDER,
                _resourceBundleLoaderProvider);
    } catch (Exception e) {
        throw new PortletException(e);
    }

    return "/view.jsp";
}

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) {//from  w  ww . j  a  v a  2 s.  c om

    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;
}

From source file:com.liferay.journal.web.internal.display.context.JournalDisplayContext.java

License:Open Source License

protected SearchContext buildSearchContext(long companyId, long groupId, List<java.lang.Long> folderIds,
        long classNameId, String ddmStructureKey, String ddmTemplateKey, String keywords,
        LinkedHashMap<String, Object> params, int start, int end, Sort sort, boolean showVersions) {

    String articleId = null;//  ww w  .  java 2s  .c o  m
    String title = null;
    String description = null;
    String content = null;
    boolean andOperator = false;

    if (Validator.isNotNull(keywords)) {
        articleId = keywords;
        title = keywords;
        description = keywords;
        content = keywords;
    } else {
        andOperator = true;
    }

    if (params != null) {
        params.put("keywords", keywords);
    }

    SearchContext searchContext = new SearchContext();

    searchContext.setAndSearch(andOperator);

    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, getStatus());
    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 });
    searchContext.setIncludeDiscussions(GetterUtil.getBoolean(params.get("includeDiscussions"), true));

    if (params != null) {
        keywords = (String) params.remove("keywords");

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

    searchContext.setAttribute("head", !showVersions);
    searchContext.setAttribute("params", params);
    searchContext.setEnd(end);
    searchContext.setFolderIds(folderIds);
    searchContext.setStart(start);

    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:com.liferay.portlet.asset.service.impl.AssetEntryLocalServiceImpl.java

License:Open Source License

@Override
public Hits search(long companyId, long[] groupIds, long userId, String className, String userName,
        String title, String description, String assetCategoryIds, String assetTagNames, int status,
        boolean andSearch, int start, int end) throws SystemException {
    try {/*ww w .  j  a v a  2 s  .  co  m*/
        SearchContext searchContext = new SearchContext();

        Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);

        assetEntriesFacet.setStatic(true);

        searchContext.addFacet(assetEntriesFacet);

        Facet scopeFacet = new ScopeFacet(searchContext);

        scopeFacet.setStatic(true);

        searchContext.addFacet(scopeFacet);

        searchContext.setAndSearch(andSearch);
        searchContext.setAssetCategoryIds(StringUtil.split(assetCategoryIds, 0L));
        searchContext.setAssetTagNames(StringUtil.split(assetTagNames));
        searchContext.setAttribute(Field.DESCRIPTION, description);
        searchContext.setAttribute(Field.TITLE, title);
        searchContext.setAttribute(Field.USER_NAME, userName);
        searchContext.setAttribute("paginationType", "regular");
        searchContext.setAttribute("status", status);
        searchContext.setCompanyId(companyId);
        searchContext.setEnd(end);
        searchContext.setEntryClassNames(getClassNames(companyId, className));
        searchContext.setGroupIds(groupIds);

        QueryConfig queryConfig = new QueryConfig();

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

        searchContext.setQueryConfig(queryConfig);

        searchContext.setStart(start);
        searchContext.setUserId(userId);

        Indexer indexer = FacetedSearcher.getInstance();

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

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

License:Open Source License

public Hits search(long companyId, long groupId, long classNameId, String articleId, String title,
        String description, String content, String type, String status, String structureId, String templateId,
        LinkedHashMap<String, Object> params, boolean andSearch, int start, int end, Sort sort)
        throws SystemException {

    try {//from   w ww.j  a v a2s  . c o  m
        Map<String, Serializable> attributes = new HashMap<String, Serializable>();

        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(Field.TYPE, type);
        attributes.put("articleId", articleId);
        attributes.put("params", params);
        attributes.put("structureId", structureId);
        attributes.put("templateId", templateId);

        SearchContext searchContext = new SearchContext();

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

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

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

        searchContext.setSorts(new Sort[] { sort });

        QueryConfig queryConfig = new QueryConfig();

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

        searchContext.setQueryConfig(queryConfig);

        searchContext.setStart(start);

        Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);

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

From source file:com.liferay.portlet.sample.service.impl.SearchLocalServiceImpl.java

License:Open Source License

/**
 * NOTE FOR DEVELOPERS://  ww w  .  j a  v  a 2  s.c  o m
 *
 * Never reference this interface directly. Always use {@link com.liferay.portlet.sample.service.SearchLocalServiceUtil} to access the search local service.
 */

public List<JournalArticle> searchWC(String structureKey, String fieldName, Serializable fieldValue,
        ThemeDisplay themeDisplay) {

    List<JournalArticle> articles = new ArrayList<JournalArticle>();

    try {
        SearchContext searchContext = new SearchContext();
        int start = QueryUtil.ALL_POS;
        int end = QueryUtil.ALL_POS;

        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();

        Map<String, Serializable> searchByFields = new LinkedHashMap<String, Serializable>();
        searchByFields.put(fieldName, fieldValue);

        params.put(SampleConstants.STRUCTURE_FIELDS, searchByFields);
        params.put(SampleConstants.STRUCTURE_KEY, structureKey);
        params.put(SampleConstants.STRUCTURE_GROUP_ID, themeDisplay.getScopeGroupId());

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

        attributes.put("params", params);

        searchContext.setAttributes(attributes);

        searchContext.setAndSearch(true);
        searchContext.setCompanyId(themeDisplay.getCompanyId());
        searchContext.setGroupIds(new long[] { PortalUtil.getSiteGroupId(themeDisplay.getScopeGroupId()) });

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

        QueryConfig queryConfig = new QueryConfig();
        queryConfig.setHighlightEnabled(false);
        queryConfig.setScoreEnabled(false);

        searchContext.setQueryConfig(queryConfig);

        searchContext.setStart(start);
        searchContext.setEnd(end);

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

        articles = getJournalArticles(indexer.search(searchContext));
    } catch (Exception e) {
        _log.error(e);
    }

    return articles;
}

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

License:Open Source License

protected Hits getHits(long companyId, long groupId, String bookTitle, String author, boolean andSearch)
        throws SystemException {

    // 1. Preparing the Search Context
    SearchContext searchContext = new SearchContext();
    searchContext.setAndSearch(andSearch);
    searchContext.setCompanyId(companyId);
    long[] groupIds = { groupId };
    searchContext.setGroupIds(groupIds);

    BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);

    appendSearchTerm(Field.TITLE, bookTitle, searchContext.isAndSearch(), searchQuery);
    appendSearchTerm("author", author, searchContext.isAndSearch(), searchQuery);

    // 2. Firing the query and getting the hits
    Hits hits = null;//from ww  w.  j a v a  2  s .c o m
    try {
        hits = SearchEngineUtil.search(searchContext, searchQuery);
    } catch (SearchException e) {
        e.printStackTrace();
    }

    return hits;
}