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

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

Introduction

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

Prototype

SearchContext

Source Link

Usage

From source file:com.liferay.mail.util.AccountIndexer.java

License:Open Source License

@Override
protected void doDelete(Account account) throws Exception {
    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(account.getCompanyId());
    searchContext.setEnd(QueryUtil.ALL_POS);
    searchContext.setSearchEngineId(getSearchEngineId());
    searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
    searchContext.setStart(QueryUtil.ALL_POS);

    BooleanQuery booleanQuery = new BooleanQueryImpl();

    booleanQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, CLASS_NAME);

    booleanQuery.addRequiredTerm("accountId", account.getAccountId());

    Hits hits = IndexSearcherHelperUtil.search(searchContext, booleanQuery);

    List<String> uids = new ArrayList<>(hits.getLength());

    for (int i = 0; i < hits.getLength(); i++) {
        Document document = hits.doc(i);

        uids.add(document.get(Field.UID));
    }//from w  w  w.j  a va 2s. c o  m

    IndexWriterHelperUtil.deleteDocuments(getSearchEngineId(), account.getCompanyId(), uids,
            isCommitImmediately());
}

From source file:com.liferay.mail.util.FolderIndexer.java

License:Open Source License

@Override
protected void doDelete(Folder folder) throws Exception {
    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(folder.getCompanyId());
    searchContext.setEnd(QueryUtil.ALL_POS);
    searchContext.setSearchEngineId(getSearchEngineId());
    searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
    searchContext.setStart(QueryUtil.ALL_POS);

    BooleanQuery booleanQuery = new BooleanQueryImpl();

    booleanQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, CLASS_NAME);

    booleanQuery.addRequiredTerm("folderId", folder.getFolderId());

    Hits hits = IndexSearcherHelperUtil.search(searchContext, booleanQuery);

    List<String> uids = new ArrayList<>(hits.getLength());

    for (int i = 0; i < hits.getLength(); i++) {
        Document document = hits.doc(i);

        uids.add(document.get(Field.UID));
    }//from   w w w  . jav  a  2  s  .co m

    IndexWriterHelperUtil.deleteDocuments(getSearchEngineId(), folder.getCompanyId(), uids,
            isCommitImmediately());
}

From source file:com.liferay.mail.util.MessageIndexer.java

License:Open Source License

@Override
protected void doDelete(Object obj) throws Exception {
    SearchContext searchContext = new SearchContext();

    searchContext.setSearchEngineId(getSearchEngineId());

    if (obj instanceof Account) {
        Account account = (Account) obj;

        searchContext.setCompanyId(account.getCompanyId());
        searchContext.setEnd(QueryUtil.ALL_POS);
        searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
        searchContext.setStart(QueryUtil.ALL_POS);

        BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

        booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

        booleanQuery.addRequiredTerm("accountId", account.getAccountId());

        Hits hits = SearchEngineUtil.search(searchContext, booleanQuery);

        List<String> uids = new ArrayList<String>(hits.getLength());

        for (int i = 0; i < hits.getLength(); i++) {
            Document document = hits.doc(i);

            uids.add(document.get(Field.UID));
        }/*w  ww .j  a  v  a2  s.  c  om*/

        SearchEngineUtil.deleteDocuments(getSearchEngineId(), account.getCompanyId(), uids,
                isCommitImmediately());
    } else if (obj instanceof Folder) {
        Folder folder = (Folder) obj;

        searchContext.setCompanyId(folder.getCompanyId());
        searchContext.setEnd(QueryUtil.ALL_POS);
        searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
        searchContext.setStart(QueryUtil.ALL_POS);

        BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

        booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

        booleanQuery.addRequiredTerm("folderId", folder.getFolderId());

        Hits hits = SearchEngineUtil.search(searchContext, booleanQuery);

        List<String> uids = new ArrayList<String>(hits.getLength());

        for (int i = 0; i < hits.getLength(); i++) {
            Document document = hits.doc(i);

            uids.add(document.get(Field.UID));
        }

        SearchEngineUtil.deleteDocuments(getSearchEngineId(), folder.getCompanyId(), uids,
                isCommitImmediately());
    } else if (obj instanceof Message) {
        Message message = (Message) obj;

        Document document = new DocumentImpl();

        document.addUID(PORTLET_ID, message.getMessageId());

        SearchEngineUtil.deleteDocument(getSearchEngineId(), message.getCompanyId(), document.get(Field.UID),
                isCommitImmediately());
    }
}

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

License:Open Source License

@Override
protected void doDelete(MBThread mbThread) throws Exception {
    SearchContext searchContext = new SearchContext();

    searchContext.setSearchEngineId(getSearchEngineId());

    deleteDocument(mbThread.getCompanyId(), mbThread.getThreadId());
}

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 keywords, int status,
        int start, int end) throws SystemException {
    try {//from   ww w . ja  v a2s . c o  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.setAttribute("paginationType", "regular");
        searchContext.setAttribute("status", status);
        searchContext.setCompanyId(companyId);
        searchContext.setEnd(end);
        searchContext.setEntryClassNames(getClassNames(companyId, className));
        searchContext.setGroupIds(groupIds);
        searchContext.setKeywords(keywords);

        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.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 {/*from 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.bookmarks.service.BookmarksFolderServiceTest.java

License:Open Source License

public void testSearch() throws Exception {
    BookmarksEntry entry = addEntry();/* w w w  .  ja  v  a  2  s .c  o  m*/

    Thread.sleep(1000);

    long companyId = entry.getCompanyId();
    long groupId = entry.getFolder().getGroupId();
    long folderId = entry.getFolderId();
    String keywords = "test";

    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(companyId);
    searchContext.setFolderIds(new long[] { folderId });
    searchContext.setGroupIds(new long[] { groupId });
    searchContext.setKeywords(keywords);

    QueryConfig queryConfig = new QueryConfig();

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

    searchContext.setQueryConfig(queryConfig);

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

    Hits hits = indexer.search(searchContext);

    assertEquals(1, hits.getLength());

    List<Document> results = hits.toList();

    for (Document doc : results) {
        assertEquals(companyId, GetterUtil.getLong(doc.get(Field.COMPANY_ID)));

        assertEquals(groupId, GetterUtil.getLong(doc.get(Field.GROUP_ID)));

        assertEqualsIgnoreCase(entry.getName(), doc.get(Field.TITLE));
        assertEquals(entry.getUrl(), doc.get(Field.URL));
        assertEqualsIgnoreCase(entry.getDescription(), doc.get(Field.DESCRIPTION));

        assertEquals(folderId, GetterUtil.getLong(doc.get("folderId")));
        assertEquals(entry.getEntryId(), GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK)));
    }

    BookmarksFolderLocalServiceUtil.deleteFolder(folderId);

    Thread.sleep(1000);

    hits = indexer.search(searchContext);

    Query query = hits.getQuery();

    assertEquals(query.toString(), 0, hits.getLength());

    addEntry();
    addEntry();
    addEntry();
    addEntry();

    Thread.sleep(1000);

    searchContext.setEnd(3);
    searchContext.setFolderIds(null);
    searchContext.setStart(1);

    hits = indexer.search(searchContext);

    assertEquals(4, hits.getLength());
    assertEquals(2, hits.getDocs().length);
}

From source file:com.liferay.portlet.documentlibrary.service.DLAppServiceTest.java

License:Open Source License

protected void search(boolean rootFolder, String keywords, boolean assertTrue) throws Exception {

    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(_fileEntry.getCompanyId());
    searchContext.setFolderIds(new long[] { _fileEntry.getFolderId() });
    searchContext.setGroupIds(new long[] { _fileEntry.getRepositoryId() });
    searchContext.setKeywords(keywords);

    QueryConfig queryConfig = new QueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);/*w  w w . j a  va2s. c o m*/

    searchContext.setQueryConfig(queryConfig);

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

    Hits hits = indexer.search(searchContext);

    List<Document> documents = hits.toList();

    boolean found = false;

    for (Document document : documents) {
        long fileEntryId = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

        if (fileEntryId == _fileEntry.getFileEntryId()) {
            found = true;

            break;
        }
    }

    String message = "Search engine could not find ";

    if (rootFolder) {
        message += "root file entry by " + keywords;
    } else {
        message += "file entry by " + keywords;
    }

    message += " using query " + hits.getQuery();

    if (assertTrue) {
        assertTrue(message, found);
    } else {
        assertFalse(message, found);
    }
}

From source file:com.liferay.portlet.documentlibrary.store.DLStoreImpl.java

License:Open Source License

public Hits search(long companyId, long userId, String portletId, long groupId, long[] repositoryIds,
        String keywords, int start, int end) throws SystemException {

    try {// w ww. ja  va2 s.c o  m
        SearchContext searchContext = new SearchContext();

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

        Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName());

        searchContext.setSearchEngineId(indexer.getSearchEngineId());

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

        BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(searchContext);

        contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);

        if (groupId > 0) {
            Group group = groupLocalService.getGroup(groupId);

            if (group.isLayout()) {
                contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);

                groupId = group.getParentGroupId();
            }

            contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
        }

        if ((repositoryIds != null) && (repositoryIds.length > 0)) {
            BooleanQuery repositoryIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

            for (long repositoryId : repositoryIds) {
                try {
                    if (userId > 0) {
                        PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

                        DLFolderPermission.check(permissionChecker, groupId, repositoryId, ActionKeys.VIEW);
                    }

                    if (repositoryId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

                        repositoryId = groupId;
                    }

                    TermQuery termQuery = TermQueryFactoryUtil.create(searchContext, "repositoryId",
                            repositoryId);

                    repositoryIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
                } catch (Exception e) {
                }
            }

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

        BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);

        searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);

        BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

        fullQuery.add(contextQuery, BooleanClauseOccur.MUST);

        if (searchQuery.clauses().size() > 0) {
            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
        }

        return SearchEngineUtil.search(searchContext, fullQuery);
    } 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 w  w  .  j a v a2 s  .  c om*/
        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);
    }
}