Created
September 7, 2016 13:27
-
-
Save SergioLarios/faa7b01482cf311479aa8901fefb2fa4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vass.index; | |
import com.liferay.portal.kernel.search.BooleanClause; | |
import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil; | |
import com.liferay.portal.kernel.search.BooleanClauseOccur; | |
import com.liferay.portal.kernel.search.BooleanQuery; | |
import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil; | |
import com.liferay.portal.kernel.search.Document; | |
import com.liferay.portal.kernel.search.Hits; | |
import com.liferay.portal.kernel.search.Indexer; | |
import com.liferay.portal.kernel.search.IndexerRegistryUtil; | |
import com.liferay.portal.kernel.search.SearchContext; | |
import com.liferay.portal.kernel.search.SearchContextFactory; | |
import com.liferay.portal.kernel.util.GetterUtil; | |
import com.liferay.portal.kernel.util.LocaleUtil; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.portlet.dynamicdatamapping.util.DDMIndexerUtil; | |
import com.liferay.portlet.journal.model.JournalArticle; | |
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil; | |
import com.vass.contents.ContentUtils; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import javax.portlet.PortletRequest; | |
public class SearchUtils { | |
/* ************************ | |
* *** Public methods **** | |
* ************************/ | |
public static SearchPagi searchByStructurePagi(PortletRequest req, | |
List<Long> structuresId, String query, int delta, int page, String langId) throws Exception{ | |
// Search Context | |
SearchContext sc = SearchContextFactory.getInstance(PortalUtil.getHttpServletRequest(req)); | |
int start = 0; | |
int end = 0; | |
if (delta > NO_PAGI && page > NO_PAGI) { | |
sc.setAttribute(PAGI_TYPE, MORE); | |
start = page * delta; | |
end = page * delta + delta - 1; | |
sc.setStart(start); | |
sc.setEnd(end); | |
} | |
Indexer indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class.getName()); | |
List<BooleanQuery> bqs = new ArrayList<>(); | |
for (Long structureId : structuresId) { | |
List<String> ifn = ContentUtils.getStructureIndexableFieldNames(structureId); | |
for (String fieldName : ifn) { | |
String structureFieldName = DDMIndexerUtil.encodeName(structureId, fieldName, | |
LocaleUtil.fromLanguageId(langId)); | |
BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(sc); | |
booleanQuery.addTerm(structureFieldName, query, true); | |
bqs.add(booleanQuery); | |
} | |
} | |
BooleanQuery innerBooleanQuery = BooleanQueryFactoryUtil.create(sc); | |
for (BooleanQuery booleanQuery : bqs) { | |
innerBooleanQuery.add(booleanQuery, BooleanClauseOccur.SHOULD); | |
} | |
BooleanClause booleanClauseGeneral = BooleanClauseFactoryUtil.create( | |
sc, innerBooleanQuery, BooleanClauseOccur.MUST.getName()); | |
sc.setBooleanClauses(new BooleanClause[] {booleanClauseGeneral}); | |
sc.setAndSearch(false); | |
Hits hits = indexer.search(sc); | |
List<JournalArticle> jas = new ArrayList<>(); | |
for (Document doc : hits.getDocs()) { | |
String artId = doc.getField(ARTICLE_ID).getValue(); | |
long scopeGroupId = GetterUtil.getLong(doc.getField(SCOPE_GROUP_ID) | |
.getValue()); | |
jas.add(JournalArticleLocalServiceUtil.getLatestArticle(scopeGroupId, artId)); | |
} | |
int total = hits.getLength(); | |
if (end > total) { | |
end = total; | |
} | |
int totalPages = 0; | |
if (total > delta) { | |
totalPages = (int) Math.ceil((double) total / delta); | |
} | |
return new SearchPagi(jas, total, delta, start, end, page, totalPages, totalPages > 0); | |
} | |
public static SearchPagi searchByStructurePagi(PortletRequest req, | |
long structureId, String query, int delta, int page, String langId) throws Exception{ | |
return searchByStructurePagi(req, Arrays.asList(structureId), query, delta, page, langId); | |
} | |
public static List<JournalArticle> searchByStructure(PortletRequest req, | |
long structureId, String query, String langId) throws Exception{ | |
SearchPagi pagi = searchByStructurePagi(req, structureId, query, NO_PAGI, NO_PAGI, langId); | |
return pagi.getJas(); | |
} | |
public static List<JournalArticle> searchByStructure(PortletRequest req, | |
List<Long> structuresId, String query, String langId) throws Exception{ | |
SearchPagi pagi = searchByStructurePagi(req, structuresId, query, NO_PAGI, NO_PAGI, langId); | |
return pagi.getJas(); | |
} | |
/* ************************ | |
* ****** Constants ******* | |
* ************************/ | |
private static final String PAGI_TYPE = "paginationType"; | |
private static final String MORE = "more"; | |
private static final String ARTICLE_ID = "articleId"; | |
private static final String SCOPE_GROUP_ID = "scopeGroupId"; | |
private static final int NO_PAGI = -2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment