Buggy code:
@ Service ( "userService" ) @ Transactional ( readOnly = true ) public class UserServiceImpl implements UserService { @ Resource private PasswordEncoder encoder ; @ Resource private UserRepository userRepository ; @ Resource private UserNotificationRepository userNotificationRepository ; @ Resource private AclSidRepository aclSidRepository ; @ Resource private RoleHierarchy roleHierarchy ; @ Resource private Client client ; @ Resource private EsAclManager esAclManager ; @ Resource private AclManager aclManager ; @ Resource private ExperimentRepository experimentRepository ; @ Resource private TraitUomRepository traitUomRepository ; @ Resource private StudyRepository studyRepository ; @ Resource private TermRepository termRepository ; @ Override @ Transactional ( readOnly = false ) public AppUser registerUserIfValid ( Registration registration , boolean userIsValid ) throws DuplicateRegistrationException { } @ Override public AppUser findUser ( Long id ) { } @ Override public AppUser findUserWithStats ( Long id ) { } private int [ ] getStats ( Long id ) { } @ Override @ Transactional ( readOnly = false ) public AppUser saveUser ( AppUser user ) { } @ Override public ExperimentPage findExperiments ( Long userId , int start , int size ) { } @ Override public TraitUomPage findPhenotypes ( Long userId , int start , int size ) { } private void initOntologies ( TraitUom traitUom ) { } @ Override public StudyPage findStudies ( Long userId , int start , int size ) { } @ Override public AppUserPage findUsers ( String searchString , ConstEnums . USER_FILTER filter , int start , int size ) { QueryBuilder searchFilter = null ; QueryBuilder adminFilter = QueryBuilders . termQuery ( "authorities" , "ROLE_ADMIN" ) ; QueryBuilder userFilter = QueryBuilders . termQuery ( "authorities" , "ROLE_USER" ) ; SearchRequestBuilder request = client . prepareSearch ( esAclManager . getIndex ( ) ) ; request . setSize ( size ) . setFrom ( start ) . setTypes ( "user" ) . setNoFields ( ) ; if ( ( searchString != null ) && ( ! ( searchString . equalsIgnoreCase ( "" ) ) ) ) { request . setQuery ( multiMatchQuery ( searchString , "firstname^3.5" , "firstname.partial^1.5" , "lastname^3.5" , "lastname.partial^1.5" , "email" ) ) ; } request . addAggregation ( org . elasticsearch . search . aggregations . AggregationBuilders . filter ( ConstEnums . USER_FILTER . ALL . name ( ) ) . filter ( QueryBuilders . matchAllQuery ( ) ) ) ; request . addAggregation ( org . elasticsearch . search . aggregations . AggregationBuilders . filter ( ADMIN . name ( ) ) . filter ( adminFilter ) ) ; request . addAggregation ( org . elasticsearch . search . aggregations . AggregationBuilders . filter ( USER . name ( ) ) . filter ( userFilter ) ) ; switch ( filter ) { case ADMIN : searchFilter = adminFilter ; break ; case USER : searchFilter = userFilter ; break ; default : if ( ( searchString == null ) || ( searchString . isEmpty ( ) ) ) <START_BUG> request . addSort ( "firstname.name" , ASC ) ; <END_BUG> } request . setPostFilter ( searchFilter ) ; SearchResponse response = request . execute ( ) . actionGet ( ) ; long totalCount = response . getHits ( ) . getTotalHits ( ) ; List < Long > idsToFetch = EsSearcher . getIdsFromResponse ( response ) ; List < AppUser > resultsFromDb = userRepository . findAll ( idsToFetch ) ; Ordering < AppUser > orderByEs = Ordering . explicit ( idsToFetch ) . onResultOf ( DomainFunctions . getUserId ( ) ) ; List < AppUser > results = orderByEs . immutableSortedCopy ( resultsFromDb ) ; Aggregations aggregations = response . getAggregations ( ) ; List < ESFacet > facets = Lists . newArrayList ( ) ; Filter filterAgg = aggregations . get ( ALL . name ( ) ) ; facets . add ( new ESFacet ( ConstEnums . USER_FILTER . ALL . name ( ) , 0 , filterAgg . getDocCount ( ) , 0 , null ) ) ; filterAgg = aggregations . get ( ADMIN . name ( ) ) ; facets . add ( new ESFacet ( ADMIN . name ( ) , 0 , filterAgg . getDocCount ( ) , 0 , null ) ) ; filterAgg = aggregations . get ( USER . name ( ) ) ; facets . add ( new ESFacet ( USER . name ( ) , 0 , filterAgg . getDocCount ( ) , 0 , null ) ) ; addStatsAndGravatarHash ( results ) ; return new AppUserPage ( results , new PageRequest ( start , size ) , totalCount , facets ) ; } private void addStatsAndGravatarHash ( List < AppUser > users ) { } private void initStats ( AppUser user ) { } private void indexUser ( AppUser user ) { } }

Fix:
request . addSort ( "firstname" , ASC ) ;