RE: How to upgrade Search related code of Liferay 7.1 to Liferay 7.4?

thumbnail
Rajesh Chaurasia, modified 2 Years ago. Regular Member Posts: 183 Join Date: 8/18/11 Recent Posts

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);
         }        
       }

 

thumbnail
Olaf Kock, modified 2 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
  • 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.

 

thumbnail
Russell Bohl, modified 2 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts

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