List of usage examples for com.liferay.portal.kernel.search SearchContext setGroupIds
public void setGroupIds(long[] groupIds)
From source file:com.slayer.service.impl.LMSBookLocalServiceImpl.java
License:Open Source License
public Hits getHits(String keyword, long companyId, long groupId) { // 1. Preparing a Search Context SearchContext searchContext = new SearchContext(); searchContext.setCompanyId(companyId); String[] CLASS_NAMES = { LMSBook.class.getName() }; searchContext.setEntryClassNames(CLASS_NAMES); long[] groupIds = { groupId }; searchContext.setGroupIds(groupIds); // 2. Preparing a Query to search BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext); String[] terms = { Field.TITLE, "author" }; try {// ww w . j a v a 2 s .c o m searchQuery.addTerms(terms, keyword); } catch (ParseException e) { e.printStackTrace(); } // 3. Firing the query to get hits Hits hits = null; try { hits = SearchEngineUtil.search(searchContext, searchQuery); } catch (SearchException e) { e.printStackTrace(); } return hits; }
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 www. j a v a2 s . c o m try { hits = SearchEngineUtil.search(searchContext, searchQuery); } catch (SearchException e) { e.printStackTrace(); } return hits; }
From source file:com.vportal.portal.search.HitsOpenSearchImpl.java
License:Open Source License
public String search(HttpServletRequest request, long groupId, long userId, String keywords, int startPage, int itemsPerPage, String format) throws SearchException { try {/*w w w . j a v a2 s .c o m*/ ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); int start = (startPage * itemsPerPage) - itemsPerPage; int end = startPage * itemsPerPage; SearchContext searchContext = SearchContextFactory.getInstance(request); searchContext.setGroupIds(new long[] { groupId }); searchContext.setEnd(end); searchContext.setKeywords(keywords); searchContext.setScopeStrict(false); searchContext.setStart(start); searchContext.setUserId(userId); addSearchAttributes(themeDisplay.getCompanyId(), searchContext, keywords); Portlet portlet = PortletLocalServiceUtil.getPortletById(themeDisplay.getCompanyId(), getPortletId()); Indexer indexer = portlet.getIndexerInstance(); Hits results = indexer.search(searchContext); String[] queryTerms = results.getQueryTerms(); int total = results.getLength(); Object[] values = addSearchResults(queryTerms, keywords, startPage, itemsPerPage, total, start, getTitle(keywords), getSearchPath(), format, themeDisplay); com.liferay.portal.kernel.xml.Document doc = (com.liferay.portal.kernel.xml.Document) values[0]; Element root = (Element) values[1]; for (int i = 0; i < results.getDocs().length; i++) { Document result = results.doc(i); String portletId = result.get(Field.PORTLET_ID); String snippet = results.snippet(i); long resultGroupId = GetterUtil.getLong(result.get(Field.GROUP_ID)); PortletURL portletURL = getPortletURL(request, portletId, resultGroupId); Summary summary = getSummary(indexer, result, snippet, portletURL); String title = summary.getTitle(); String url = getURL(themeDisplay, resultGroupId, result, portletURL); Date modifedDate = result.getDate(Field.MODIFIED); String content = summary.getContent(); String[] tags = new String[0]; Field assetTagNamesField = result.getFields().get(Field.ASSET_TAG_NAMES); if (assetTagNamesField != null) { tags = assetTagNamesField.getValues(); } double ratings = 0.0; String entryClassName = result.get(Field.ENTRY_CLASS_NAME); long entryClassPK = GetterUtil.getLong(result.get(Field.ENTRY_CLASS_PK)); if ((Validator.isNotNull(entryClassName)) && (entryClassPK > 0)) { RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(entryClassName, entryClassPK); ratings = stats.getTotalScore(); } double score = results.score(i); addSearchResult(root, resultGroupId, entryClassName, entryClassPK, title, url, modifedDate, content, tags, ratings, score, format); } if (_log.isDebugEnabled()) { _log.debug("Return\n" + doc.asXML()); } return doc.asXML(); } catch (Exception e) { throw new SearchException(e); } }