@Test public void testSearchAndDeleteFolderAndSearch() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId()); BookmarksFolder folder = BookmarksTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString()); BookmarksEntry entry = BookmarksTestUtil.addEntry(folder.getFolderId(), true, serviceContext); long companyId = entry.getCompanyId(); long groupId = entry.getFolder().getGroupId(); long folderId = entry.getFolderId(); String keywords = "test"; SearchContext searchContext = BookmarksTestUtil.getSearchContext(companyId, groupId, folderId, keywords); Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class); Hits hits = indexer.search(searchContext); Assert.assertEquals(1, hits.getLength()); BookmarksFolderLocalServiceUtil.deleteFolder(folderId); hits = indexer.search(searchContext); Query query = hits.getQuery(); Assert.assertEquals(query.toString(), 0, hits.getLength()); }
@Override public BaseModelSearchResult<UserGroup> searchUserGroups( long companyId, String name, String description, LinkedHashMap<String, Object> params, boolean andSearch, int start, int end, Sort sort) throws PortalException { Indexer<UserGroup> indexer = IndexerRegistryUtil.nullSafeGetIndexer(UserGroup.class); SearchContext searchContext = buildSearchContext(companyId, name, description, params, andSearch, start, end, sort); for (int i = 0; i < 10; i++) { Hits hits = indexer.search(searchContext); List<UserGroup> userGroups = UsersAdminUtil.getUserGroups(hits); if (userGroups != null) { return new BaseModelSearchResult<>(userGroups, hits.getLength()); } } throw new SearchException("Unable to fix the search index after 10 attempts"); }
public SearchContainer<MBMessage> getCommentsSearchContainer() throws PortalException { SearchContainer<MBMessage> searchContainer = new SearchContainer( _liferayPortletRequest, _liferayPortletResponse.createRenderURL(), null, null); SearchContext searchContext = SearchContextFactory.getInstance(_liferayPortletRequest.getHttpServletRequest()); searchContext.setAttribute( Field.CLASS_NAME_ID, PortalUtil.getClassNameId(JournalArticle.class)); searchContext.setAttribute("discussion", true); List<MBMessage> mbMessages = new ArrayList<>(); Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class); Hits hits = indexer.search(searchContext); for (Document document : hits.getDocs()) { long entryClassPK = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK)); MBMessage mbMessage = MBMessageLocalServiceUtil.fetchMBMessage(entryClassPK); mbMessages.add(mbMessage); } searchContainer.setResults(mbMessages); searchContainer.setTotal(hits.getLength()); return searchContainer; }
@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 indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class); searchContext.setEnd(3); 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); }
@Test public void testSearchAndVerifyDocs() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId()); BookmarksFolder folder = BookmarksTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString()); BookmarksEntry entry = BookmarksTestUtil.addEntry(folder.getFolderId(), true, serviceContext); SearchContext searchContext = BookmarksTestUtil.getSearchContext( entry.getCompanyId(), entry.getGroupId(), entry.getFolderId(), "test"); Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class); Hits hits = indexer.search(searchContext); Assert.assertEquals(1, hits.getLength()); List<Document> results = hits.toList(); for (Document doc : results) { Assert.assertEquals(entry.getCompanyId(), GetterUtil.getLong(doc.get(Field.COMPANY_ID))); Assert.assertEquals(BookmarksEntry.class.getName(), doc.get(Field.ENTRY_CLASS_NAME)); Assert.assertEquals(entry.getEntryId(), GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK))); AssertUtils.assertEqualsIgnoreCase(entry.getName(), doc.get(Field.TITLE)); Assert.assertEquals(entry.getUrl(), doc.get(Field.URL)); } }
public Hits search(SearchContext searchContext) throws SearchException { Indexer indexer = DLSearcher.getInstance(); searchContext.setSearchEngineId(indexer.getSearchEngineId()); return indexer.search(searchContext); }
protected int searchBaseModelsCount(Class<?> clazz, long groupId, SearchContext searchContext) throws Exception { Indexer indexer = IndexerRegistryUtil.getIndexer(clazz); searchContext.setGroupIds(new long[] {groupId}); Hits results = indexer.search(searchContext); return results.getLength(); }
protected void search( FileEntry fileEntry, boolean rootFolder, String keywords, boolean assertTrue) 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 = new QueryConfig(); queryConfig.setHighlightEnabled(false); queryConfig.setScoreEnabled(false); searchContext.setQueryConfig(queryConfig); Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName()); 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) { Assert.assertTrue(message, found); } else { Assert.assertFalse(message, found); } }
public Hits search( long companyId, long[] groupIds, long userId, String className, String keywords, int status, int start, int end) throws SystemException { try { 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(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); } }
protected BaseModelSearchResult<AssetVocabulary> searchVocabularies(SearchContext searchContext) throws PortalException { Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(AssetVocabulary.class); for (int i = 0; i < 10; i++) { Hits hits = indexer.search(searchContext); List<AssetVocabulary> vocabularies = getVocabularies(hits); if (vocabularies != null) { return new BaseModelSearchResult<>(vocabularies, hits.getLength()); } } throw new SearchException("Unable to fix the search index after 10 attempts"); }
@Override public Hits search( long groupId, long userId, long creatorUserId, long startDate, long endDate, int status, int start, int end) throws PortalException { Indexer<MBThread> indexer = IndexerRegistryUtil.getIndexer(MBThread.class.getName()); SearchContext searchContext = new SearchContext(); searchContext.setAttribute(Field.STATUS, status); if (endDate > 0) { searchContext.setAttribute("endDate", endDate); } searchContext.setAttribute("paginationType", "none"); if (creatorUserId > 0) { searchContext.setAttribute("participantUserId", String.valueOf(creatorUserId)); } if (startDate > 0) { searchContext.setAttribute("startDate", startDate); } Group group = groupLocalService.getGroup(groupId); searchContext.setCompanyId(group.getCompanyId()); searchContext.setEnd(end); searchContext.setGroupIds(new long[] {groupId}); searchContext.setSorts(new Sort("lastPostDate", true)); searchContext.setStart(start); searchContext.setUserId(userId); return indexer.search(searchContext); }
@Test public void testSearch() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId()); BookmarksFolder folder = BookmarksTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString()); BookmarksEntry entry = BookmarksTestUtil.addEntry(folder.getFolderId(), true, serviceContext); SearchContext searchContext = BookmarksTestUtil.getSearchContext( entry.getCompanyId(), entry.getGroupId(), entry.getFolderId(), "test"); Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class); Hits hits = indexer.search(searchContext); Assert.assertEquals(1, hits.getLength()); }
protected void search(boolean rootFolder, String keywords) 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); searchContext.setQueryConfig(queryConfig); Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class); List<Document> documents = indexer.search(searchContext).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; } assertTrue(message, found); }
/** * Returns an ordered range of all the user groups that match the name and description. It is * preferable to use this method instead of the non-indexed version whenever possible for * performance reasons. * * <p>Useful when paginating results. Returns a maximum of <code>end - * start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are * indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting * both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full * result set. * * @param companyId the primary key of the user group's company * @param name the user group's name (optionally <code>null</code>) * @param description the user group's description (optionally <code>null</code>) * @param params the finder params (optionally <code>null</code>). For more information see {@link * com.liferay.portlet.usergroupsadmin.util.UserGroupIndexer} * @param andSearch whether every field must match its keywords or just one field * @param start the lower bound of the range of user groups to return * @param end the upper bound of the range of user groups to return (not inclusive) * @param sort the field and direction by which to sort (optionally <code>null</code>) * @return the matching user groups ordered by sort * @see com.liferay.portal.service.persistence.UserGroupFinder */ @Override public Hits search( long companyId, String name, String description, LinkedHashMap<String, Object> params, boolean andSearch, int start, int end, Sort sort) { try { Indexer<UserGroup> indexer = IndexerRegistryUtil.nullSafeGetIndexer(UserGroup.class); SearchContext searchContext = buildSearchContext(companyId, name, description, params, andSearch, start, end, sort); return indexer.search(searchContext); } catch (Exception e) { throw new SystemException(e); } }
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 { 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(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); } }