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 upgrade Search related code of Liferay 7.1 to Liferay 7.4?
Hi ,
I wanted to know how to upgrade he below code snippet of Liferay 7.1 to Liferay 7.4.Please suggest.
public final void serveResource(ResourceRequest request, ResourceResponse response) throws IOException {
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
String keywords = ParamUtil.getString(request, "keywords");
SearchContext searchContext = new SearchContext();
searchContext.setCompanyId(themeDisplay.getCompanyId());
searchContext.setEntryClassNames(new String[] {User.class.getName()});
BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);
searchQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, User.class.getName());
// Setting up filters.
fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
//Search for users with Role Mentor
BooleanQuery userRoleIdQry = BooleanQueryFactoryUtil.create(searchContext);
Role role = RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId() ,"UIS_ROLE");
userRoleIdQry.addRequiredTerm("roleIds", role.getRoleId());
fullQuery.add(userRoleIdQry, BooleanClauseOccur.MUST);
// keyword as username
BooleanQuery userFullNameQuery = BooleanQueryFactoryUtil.create(searchContext);
userFullNameQuery.addRequiredTerm(FULL_NAME, keywords, true);
fullQuery.add(userFullNameQuery, BooleanClauseOccur.MUST);
// Exclude inactive users
BooleanQuery statusQuery = BooleanQueryFactoryUtil.create(searchContext);
statusQuery.addExactTerm(Field.STATUS, WorkflowConstants.STATUS_INACTIVE);
fullQuery.add(statusQuery, BooleanClauseOccur.MUST_NOT);
try {
LOG.debug("******************************fullQuery*******************************************"+fullQuery);
Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
if (Validator.isNull(hits) || hits.getLength() == 0)
LOG.debug("No records match search criteria");
JSONArray results = JSONFactoryUtil.createJSONArray();
for (Document document : hits.getDocs()) {
Long casePK = Long.parseLong(document.getField(Field.ENTRY_CLASS_PK).getValue());
JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
jsonObj.put("id", casePK);
jsonObj.put(Field.NAME, document.getField(FULL_NAME).getValue());
results.put(jsonObj);
}
response.getWriter().write(results.toString());
} catch (SearchException e) {
LOG.error("Error while searching users ", e);
}
}
- How does that code behave?
- How do you expect it to behave?
- Did the API change dramatically or is there a single line that doesn't compile?
In general, you might want to look at the "Breaking Changes" documentation for the intermediate and target versions and identify if they tell you what has changed, why it did, and what to do.
First, check out this 7.2 article on search queries and filters--you'll see that the way to build your queries has changed. It should sitll be valid for 7.4
https://help.liferay.com/hc/en-us/articles/360029046411-Building-Search-Queries-and-Filters
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™