0

I am using the Apache Directory API to query an OpenLDAP server. I think this code should return the results sorted by sn but they are returned in random order.

    SearchRequest req = new SearchRequestImpl();
    req.setScope(SearchScope.ONELEVEL);
    req.addAttributes("*");
    req.setTimeLimit(0);
    req.setBase(searchDn);
    req.setFilter("(objectclass=posixAccount)");

    SortRequest sortRequest = new SortRequestControlImpl();
    sortRequest.addSortKey(new SortKey("sn"));
    req.addControl(sortRequest);

    try (EntryCursor cursor = new EntryCursorImpl(connection.search(req))) {        
        for (Entry entry : cursor) {
            System.out.println("sn: " + entry.get("sn").getString());                
        }
    }

The output of this code from the data in my test LDAP Server is:

sn: Trainee 
sn: Admin 
sn: User 
sn: Supervisor 
sn: Supervisor

I based the code on this Integration test https://github.com/apache/directory-server/blob/master/server-integ/src/test/java/org/apache/directory/server/operations/search/SortedSearchIT.java and I can't see what I have done wrong.

Can anyone offer any advice? Thanks.

7
  • Could you post the returned list? Just so I can see what this is producing Commented Mar 6, 2019 at 19:23
  • I've updated the question with the output from my test server. Also the order doesn't change if I do call sk.setReverseOrder( true ); on the SortKey, so I think the sorting is just being ignored. Commented Mar 6, 2019 at 20:32
  • Have you tried without wrapping the search result in a EntryCursorImpl? Commented Mar 6, 2019 at 20:33
  • I just tried this - same result try (SearchCursor cursor = connection.search(req)) { SearchResultEntry se = (SearchResultEntry) cursor.get(); Entry entry = se.getEntry(); System.out.println("sn: " + entry.get("sn").getString()); } Commented Mar 6, 2019 at 20:38
  • Yes. Same result? Commented Mar 6, 2019 at 20:39

1 Answer 1

0

After much digging, I had to do two things to get this to work.

First add sssvlv support to my OpenLDAP server

This is the ldif

dn: cn=module{0}, cn=config
changetype: modify
add: olcModuleLoad
olcModuleLoad: sssvlv.la

dn: olcOverlay=sssvlv,olcDatabase={1}hdb,cn=config
changetype: add
objectClass: olcSssVlvConfig
olcSssVlvMax: 10
olcSssVlvMaxKeys: 5

Then I had to specify the Matching Rule ID for the sort key

SortKey sk = new SortKey( "sn", SchemaConstants.NUMERIC_STRING_ORDERING_MATCH_MR_OID);

Hopefully this will help someone!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.