Liferay Search with multiple portlets


Greetings from Felix!!!!

This is about Liferay Search, it is tricky and one of the most complex part of the Liferay Portal!

Till today I was thinking that Liferay didn't provide any way to search content for selective portlets, but it is not true.

For e.g., if I want to search only for Document Library and Journal Content in a single service call, it is possible with Liferay.

Just follow method (Liferay Version 6.0.4 Community Edition) Liferay Portal src

There is a code which sets the PORTLET_ID in the Query.

if (Validator.isNotNull(portletId)) {
contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);

Just comment out this part and add the following part

BooleanQuery portletQuery = BooleanQueryFactoryUtil.create();
portletQuery.addTerm(Field.PORTLET_ID, portletId);

And once, fullQuery is created, add this portletQuery to the fullQuery.

fullQuery.add(portletQuery, BooleanClauseOccur.MUST);

Now, comes the real part!

If you want to search for multiple portlets, just create a String of portletIds and put "OR" operator in between.

For e.g., if you want to search only for JournalArticle and Document Library, create a portletId value as "15 OR 20",where "15" is a portletId of Journal and "20" is a portletId of Document Library.

This will return search Hits for given portletIds.

This is very useful, as developer does not have to worry about the custom pagination and custom search containers.

Do send me your comments!

No comments:

Post a Comment