RE: searchContainer doesn't change after removing an object from User List

JAOUAD MOUAOU, modified 6 Years ago. New Member Posts: 9 Join Date: 9/27/17 Recent Posts

I'm creating a hook for Site Members Directory. My requirement is to filter the search container based on a String value. I was able to remove the objects that I don't want and return new User List with a new size. But the problem is that the results show just the first page of results. For example, let's say I run a search and return 20 objects and after the filter, I return 15 objects. In my view page, it will only display the 1st page and no pagination. I have set delta to 5 so it should show 3 pages but it shows only one.

 

<%
userSearchContainer.setDelta(5);

if (searchTerms.isAdvancedSearch()) {
    total = UserLocalServiceUtil.searchCount(company.getCompanyId(), searchTerms.getFirstName(), searchTerms.getMiddleName(), searchTerms.getLastName(), searchTerms.getScreenName(), searchTerms.getEmailAddress(), searchTerms.getStatus(), userParams, searchTerms.isAndOperator());

    userSearchContainer.setTotal(total);

    results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getFirstName(), searchTerms.getMiddleName(), searchTerms.getLastName(), searchTerms.getScreenName(), searchTerms.getEmailAddress(), searchTerms.getStatus(), userParams, searchTerms.isAndOperator(), userSearchContainer.getStart(), userSearchContainer.getEnd(), userSearchContainer.getOrderByComparator());

} else if (Validator.isNotNull(searchTerms.getKeywords())){
    total = UserLocalServiceUtil.searchCount(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams);

    userSearchContainer.setTotal(total);
   
    results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams, userSearchContainer.getStart(), userSearchContainer.getEnd(), userSearchContainer.getOrderByComparator());
}
    //filter search results
    if(results != null){
     Iterator<User> iterator = results.iterator();
        while(iterator.hasNext()){
            User res = iterator.next(); %>
            String TestValue = "test123";
            <%
            if(!res.getScreenName().equals(TestValue)){
                iterator.remove();
        }
      }
    }



System.out.println("New List Total " + results.size());    

//set new list size
userSearchContainer.setTotal(results.size());

//set new list results
userSearchContainer.setResults(results);

%>

 

thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts

That's because you set the total to the current page's results. So it thinks there's no more results and doesn't render pagination.

 

This probably isn't the best way to filter because some pages may have differing amount of results depending if there's a user to filter or not. I think you could do something like this to add to the query:

userParams.put("test", new CustomSQLParam("WHERE User_.screeName != 'sadf'", StringPool.BLANK));
JAOUAD MOUAOU, modified 6 Years ago. New Member Posts: 9 Join Date: 9/27/17 Recent Posts

Thanks for your reply, Amos. I've tried your solution and I can filter based on some value, but still doesn't get me what I want. The way the new search directory work is it runs the search first on the Liferay database and after that, I make another connection to an external database that uses screenName as the primary key for users. After that I do a comparison between Liferay database and another external database and bring the matching rows and display them on the search container. 

 

If I do something like this it works just fine on development env which has just a few users 100 maybe, but when I deploy the hook to pre-prod which has over 100,000 users the connection times out or it takes longer to respond if there are just few return values.

 

results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams, <span style="color: rgb(255,0,0);">-1</span>, <span style="color: rgb(255,0,0);">-1</span>, userSearchContainer.getOrderByComparator());
JAOUAD MOUAOU, modified 6 Years ago. New Member Posts: 9 Join Date: 9/27/17 Recent Posts

Any help guys.

thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
If you're doing a search with -1, -1 as start, end, it's going to return all users which of course be slow if you have over 100,000. So you've got to find a more efficient way to do this. I would suggest, (assuming the comparison can be done on user update) to add a new field in the search index on the user document probably with a mode listener . Then add it to search via a Indexer Post Processor .
JAOUAD MOUAOU, modified 6 Years ago. New Member Posts: 9 Join Date: 9/27/17 Recent Posts

Thanks, I've used your first approach, but on usersRoles and it seems to work.

if (roleid .equals(9898)) {
    userParams.put("usersRoles", Long.parseLong(roleid));
}