Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: how to fetch latest version of each document using ElasticSearch Quer
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.
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.
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:
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.
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.
Thank You Very Much Amos Fong for your response
.
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??

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??
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
https://github.com/liferay/liferay-portal/blob/master/CONTRIBUTING.markdown
Also, feel free to send a github PR with changes to DLFileEntryIndexer so it'll be there in the future

https://github.com/liferay/liferay-portal/blob/master/CONTRIBUTING.markdown
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™