Search code examples
luceneliferayliferay-6

Liferay custom search only last versions of journal article


I wrote a lucene search. Everything is fine but search is returning all versions of each journal article. How can I make any restrictions to this search so it can return only last versions of journal articles?

This is piece of search code:

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));
        SearchContext searchContext = SearchContextFactory.getInstance(httpServletRequest);
        BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
        BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);

searchQuery.addRequiredTerm(Field.TITLE, actionRequest.getParameter("searchText"));
            if (actionRequest.getParameter("searchType").equals("content")) {
                searchQuery.addRequiredTerm(Field.TYPE, "general");
                searchQuery.addRequiredTerm("ddmStructureKey", "");
            }
.
.
.
fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
searchContext.setStart(QueryUtil.ALL_POS);
searchContext.setEnd(QueryUtil.ALL_POS);
searchContext.setAttribute("paginationType", "none");
Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
List<Document> docs = hits.toList();

I tried to get search logs when I was searching with portlet 3 (search portlet) for word "datumu", this is what I got:

Search query +(+(companyId:10154) +((+(entryClassName:com.liferay.portal.model.User) +(status:0)) (+(entryClassName:com.liferay.portlet.bookmarks.model.BookmarksEntry) +(status:0)) (+(entryClassName:com.liferay.portlet.bookmarks.model.BookmarksFolder) +(status:0)) (+(entryClassName:com.liferay.portlet.blogs.model.BlogsEntry) +(status:0)) (+(entryClassName:com.liferay.portlet.documentlibrary.model.DLFileEntry) +(status:0) +(hidden:false)) (+(entryClassName:com.liferay.portlet.documentlibrary.model.DLFolder) +(status:0) +(hidden:false)) (+(entryClassName:com.liferay.portlet.journal.model.JournalArticle) +(status:0) +(head:true)) (+(entryClassName:com.liferay.portlet.journal.model.JournalFolder) +(status:0)) (+(entryClassName:com.liferay.portlet.messageboards.model.MBMessage) +(status:0) +(discussion:false)) (+(entryClassName:com.liferay.portlet.wiki.model.WikiPage) +(status:0)))) +(assetCategoryTitles:datumu assetCategoryTitles_sk_SK:datumu assetTagNames:datumu comments:datumu content:datumu description:datumu properties:datumu title:datumu url:datumu userName:datumu city:datumu country:datumu emailAddress:datumu firstName:datumu fullName:datumu lastName:datumu middleName:datumu region:datumu screenName:datumu street:datumu zip:datumu ddmContent:datumu extension:datumu fileEntryTypeId:datumu path:datumu classPK:datumu content_sk_SK:datumu description_sk_SK:datumu entryClassPK:datumu title_sk_SK:datumu type:datumu articleId:datumu

So which clause is causing restriction so that this search is returning only last version of journal article?


Solution

  • searchQuery.addRequiredTerm("head", Boolean.TRUE);
    

    this should do the trick. in my case it helped