gipouf
gipouf

Reputation: 1241

Spring Data Neo4j - findAll() doesn't return sorted results

I'm trying to use findAll() of the GraphRepository using PageRequest with sorting properties, but for some reson my data is not returned in a sorted way. Here is my code:

public class Playground {

    /**
     * @param args
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DefaultApplicationConfig.class);
        SocialRescueManager manager = (SocialRescueManager)context.getBean("socialRescueManager");

        manager.Civilians.deleteAll();

        manager.Civilians.save(new Civilian("B", "lastName1", Nationality.USA, EyeColor.BLUE, HairColor.BLOND, 180, new DateTime(1984, 1 , 9, 0, 0)));
        manager.Civilians.save(new Civilian("C", "lastName2", Nationality.USA, EyeColor.GREEN, HairColor.BLACK, 170, new DateTime(1985, 6 , 9, 0, 0)));
        manager.Civilians.save(new Civilian("A", "lastName3", Nationality.USA, EyeColor.BLUE, HairColor.BLOND, 175, new DateTime(1990, 10 , 23, 0, 0)));

        Pageable pageable = new PageRequest(0, 5, Direction.DESC, "firstName");

        Page<Civilian> results = manager.Civilians.findAll(pageable);

        for(Iterator<Civilian> i = results.iterator(); i.hasNext(); ) {
            Civilian civilian = i.next();
            System.out.println(civilian.getFirstName());
            System.out.println(civilian.getDateOfBirth());
        }

        context.close();
    }
}

My Civilian class looks like this:

@NodeEntity
public class Civilian extends Profile {

    @GraphProperty(propertyType = Long.class)
    DateTime dateOfBirth;

    public Civilian() {
    }

    public Civilian(String firstName, String lastName, Nationality nationality, EyeColor eyeColor, HairColor hairColor, int height, DateTime dateOfBirth) {
        super(firstName, lastName, nationality, eyeColor, hairColor, height);
        this.setDateOfBirth(dateOfBirth);
    }

    public DateTime getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(DateTime dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }   
}

The Civilian class extends the Profile class, which looks like this:

public abstract class Profile extends AbstractEntity {

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "firstName")
    String firstName;

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "lastName")
    String lastName;

    @Indexed
    EyeColor eyeColor;

    @Indexed    
    HairColor hairColor;

    @Indexed
    Nationality nationality;

    @Indexed
    int height;

    public Profile() {

    }

    public Profile(String firstName, String lastName, Nationality nationality, EyeColor eyeColor, HairColor hairColor, int height) {
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.setNationality(nationality);
        this.setHairColor(hairColor);
        this.setEyeColor(eyeColor);
        this.setHeight(height);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public EyeColor getEyeColor() {
        return eyeColor;
    }

    public void setEyeColor(EyeColor eyeColor) {
        this.eyeColor = eyeColor;
    }

    public HairColor getHairColor() {
        return hairColor;
    }

    public void setHairColor(HairColor hairColor) {
        this.hairColor = hairColor;
    }

    public Nationality getNationality() {
        return nationality;
    }

    public void setNationality(Nationality nationality) {
        this.nationality = nationality;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

So, the results of my Main() method are as follows:

B
1984-01-09T00:00:00.000+02:00
C
1985-06-09T00:00:00.000+03:00
A
1990-10-23T00:00:00.000+02:00

But as you can see, i set my PageRequest object to sort the results by the "firstName" property, in DESC order, so i would expect to get C printed first, then B, and last A.

What am i doing wrong?

Thanks.

Upvotes: 0

Views: 552

Answers (1)

tstorms
tstorms

Reputation: 5001

The javadoc of CRUDRepository.findAll(Pageable) states:

NOTE: the sorting is not yet implemented

You'll have to contribute a fix, or you might as well consider using a cypher query with sorting enabled. E.g.

START n=node:firstName('firstName:abc')
RETURN n
ORDER BY n.firstName DESC

Upvotes: 3

Related Questions