List of usage examples for com.liferay.portal.kernel.search Hits getQuery
public Query getQuery();
From source file:com.liferay.bookmarks.service.test.BookmarksFolderServiceTest.java
License:Open Source License
@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<BookmarksEntry> 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()); }
From source file:com.liferay.portlet.bookmarks.service.BookmarksFolderServiceTest.java
License:Open Source License
public void testSearch() throws Exception { BookmarksEntry entry = addEntry();/*from w ww . j a v a2s . co 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);/* ww w .ja v a2s. 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); } }