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

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

Introduction

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

Prototype

public void setFolderIds(long[] folderIds) 

Source Link

Usage

From source file:com.liferay.bookmarks.service.test.BookmarksFolderServiceTest.java

License:Open Source License

@Test
public void testSearchRange() throws Exception {
    BookmarksEntry entry = BookmarksTestUtil.addEntry(_group.getGroupId(), true);

    BookmarksTestUtil.addEntry(_group.getGroupId(), true);
    BookmarksTestUtil.addEntry(_group.getGroupId(), true);
    BookmarksTestUtil.addEntry(_group.getGroupId(), true);

    SearchContext searchContext = BookmarksTestUtil.getSearchContext(_group.getCompanyId(), _group.getGroupId(),
            entry.getFolderId(), "test");

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

    searchContext.setEnd(3);/* ww w  . java 2 s  .  c o m*/
    searchContext.setFolderIds((long[]) null);
    searchContext.setStart(1);

    Hits hits = indexer.search(searchContext);

    Assert.assertEquals(4, hits.getLength());

    Document[] documents = hits.getDocs();

    Assert.assertEquals(2, documents.length);
}

From source file:com.liferay.bookmarks.util.test.BookmarksTestUtil.java

License:Open Source License

public static SearchContext getSearchContext(long companyId, long groupId, long folderId, String keywords,
        boolean highlight, boolean score) {

    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(highlight);
    queryConfig.setScoreEnabled(score);/*from   w w  w  .j  a  v a  2  s .c om*/

    searchContext.setQueryConfig(queryConfig);

    return searchContext;
}

From source file:com.liferay.document.library.repository.cmis.search.BaseCmisSearchQueryBuilderTest.java

License:Open Source License

protected String buildFolderQuery(boolean searchSubfolders) throws Exception {

    SearchContext searchContext = getSearchContext();

    searchContext.setFolderIds(new long[] { _DL_FOLDER_ID });
    searchContext.setKeywords("test");

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setAttribute("capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());
    queryConfig.setSearchSubfolders(searchSubfolders);

    return buildQuery(searchContext);
}

From source file:com.liferay.document.library.service.test.DLAppServiceTest.java

License:Open Source License

protected static void search(FileEntry fileEntry, String keywords, boolean expected) throws Exception {

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute("paginationType", "regular");
    searchContext.setCompanyId(fileEntry.getCompanyId());
    searchContext.setFolderIds(new long[] { fileEntry.getFolderId() });
    searchContext.setGroupIds(new long[] { fileEntry.getRepositoryId() });
    searchContext.setKeywords(keywords);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);/*from   ww w . ja v a  2  s  .c  o  m*/

    Indexer<DLFileEntry> indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName());

    Hits hits = indexer.search(searchContext);

    boolean found = false;

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

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

            break;
        }
    }

    Assert.assertEquals(hits.toString(), expected, found);
}

From source file:com.liferay.journal.search.test.JournalIndexerTest.java

License:Open Source License

protected void moveArticle(boolean moveToTrash) throws Exception {
    SearchContext searchContext1 = SearchContextTestUtil.getSearchContext(_group.getGroupId());

    searchContext1.setKeywords("Architectural");

    JournalFolder folder1 = JournalTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString());

    searchContext1.setFolderIds(new long[] { folder1.getFolderId() });

    assertSearchCount(0, _group.getGroupId(), searchContext1);

    SearchContext searchContext2 = SearchContextTestUtil.getSearchContext(_group.getGroupId());

    searchContext2.setKeywords("Architectural");

    JournalFolder folder2 = JournalTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString());

    searchContext2.setFolderIds(new long[] { folder2.getFolderId() });

    assertSearchCount(0, _group.getGroupId(), searchContext2);

    JournalArticle article = JournalTestUtil.addArticleWithWorkflow(_group.getGroupId(), folder1.getFolderId(),
            "title", "Liferay Architectural Approach", true);

    assertSearchCount(1, _group.getGroupId(), searchContext1);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    if (moveToTrash) {
        JournalFolderLocalServiceUtil.moveFolderToTrash(TestPropsValues.getUserId(), folder1.getFolderId());

        assertSearchCount(0, _group.getGroupId(), searchContext1);

        article = JournalArticleLocalServiceUtil.getArticle(article.getId());

        JournalArticleLocalServiceUtil.moveArticleFromTrash(TestPropsValues.getUserId(), _group.getGroupId(),
                article, folder2.getFolderId(), serviceContext);
    } else {/* www  .j  a v  a  2s.c om*/
        JournalArticleLocalServiceUtil.moveArticle(_group.getGroupId(), article.getArticleId(),
                folder2.getFolderId(), serviceContext);
    }

    assertSearchCount(0, _group.getGroupId(), searchContext1);
    assertSearchCount(1, _group.getGroupId(), searchContext2);
}

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) {/*  ww  w  . j a  va 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;
}

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;//from   ww  w .j  a  va2s. 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.bookmarks.service.BookmarksFolderServiceTest.java

License:Open Source License

public void testSearch() throws Exception {
    BookmarksEntry entry = addEntry();/* w w  w.  j  a  v  a2 s.  com*/

    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  ava2s .  co  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);
    }
}