RE: how to fetch latest version of each document using ElasticSearch Quer

Bhargav Vandana, modified 6 Years ago. New Member Posts: 5 Join Date: 3/7/19 Recent Posts
Hi,

I am using Liferay 7.0

My requirement is to fetch latest version of document (journal article and document media).

i have wirtten below query

SearchContext searchContext = SearchContextFactory.getInstance(PortalUtil.getHttpServletRequest(renderRequest));
searchContext.setCompanyId(PortalUtil.getCompanyId(renderRequest));
searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
searchContext.setAssetTagNames(new String[]{ "spec-content", "gen-content" });
searchContext.setLocale(Locale.US);

searchContext.setAttribute(Field.STATUS, new int[] {0,1,2,7});
searchContext.setAttribute("head", false);

searchContext.setAttribute("paginationType", "none");


Indexer<?> indexer = IndexerRegistryUtil.getIndexer(JournalArticle.class);
try {

Hits hits = indexer.search(searchContext, "*");

} catch (SearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



It feteches all versions of each Document.

o/p is

ARTICLE ID >>>>> 34218 STATUS >>>>>>>>> 7 VERSION >>>>>>>> 1.0 CONTENT >>>>>>>> Contest - 03
ARTICLE ID >>>>> 34190 STATUS >>>>>>>>> 2 VERSION >>>>>>>> 1.1 CONTENT >>>>>>>> Contest - 01 (DRAFT)
ARTICLE ID >>>>> 34190 STATUS >>>>>>>>> 0 VERSION >>>>>>>> 1.0 CONTENT >>>>>>>> Contest - 01
ARTICLE ID >>>>> 34203 STATUS >>>>>>>>> 2 VERSION >>>>>>>> 1.0 CONTENT >>>>>>>> Contest - 02 (Draft)


How to prepare my search context to fullfill my requirement?
Please suggest.
thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
You may want to take a look at how articles are indexed to get a better idea how to search them:
https://github.com/liferay/com-liferay-journal/blob/7.0.x/journal-service/src/main/java/com/liferay/journal/search/JournalArticleIndexer.java

Depending on what you're looking for, I think you either want to set head or latest (or both) to true:
searchContext.setAttribute("head", Boolean.TRUE);
searchContext.setAttribute("latest", Boolean.TRUE);

Check out JournalUtil to see how they are defined:
https://github.com/liferay/com-liferay-journal/blob/7.0.x/journal-service/src/main/java/com/liferay/journal/util/impl/JournalUtil.java

If a OOTB field doesn't fit your criteria, then you can create a IndexerPostProcessor to add your own fields to search by.
Bhargav Vandana, modified 6 Years ago. New Member Posts: 5 Join Date: 3/7/19 Recent Posts
Thank You Very Much Amos Fong for your response emoticon.

But in my case i have to fetch only the latest version of the document whether its status is draft/pending/scheduled/approved.

For example, I created journal article which status is approved and the version is 1.0. Now I modified the same article and its status is draft and version is 1.1.

Now i have to query and fetch the latest version of the article which version is 1.1 and status is draft in my case.

I am using Liferay 7.0 and elasticsearch version is 2.x. I haven't seen "latest : true" in elastic search results.

do i need to write IndexPostProcessor and add new field called "latest" while creating document or we have any alternate way to fullfill my requirement??
thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
After a quick look at the DLFileEntryIndexer, it looks like "latest" isn't there. So yea, I think you'll have to write a IndexPostProcessor for document library files.

Also, feel free to send a github PR with changes to DLFileEntryIndexer so it'll be there in the future emoticon
https://github.com/liferay/liferay-portal/blob/master/CONTRIBUTING.markdown