You are viewing a plain text version of this content. The canonical link for it is here.
Posted to api@directory.apache.org by Tamsin Slinn <ta...@gmail.com> on 2019/03/07 09:40:49 UTC

How to do a sorted search using Apache Directory API

Hi,
I hope this is an OK question to post to the list.

I'm running the code below against OpenLDAP 2.4.40. I based the code on an
integration test for SortRequestControl. However, this code does not return
the entries in the order I expect (alphabetical by sn). Can you tell me
what I am missing?
Thanks for any help!

    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 (SearchCursor cursor = connection.search(req)) {
         while (cursor.next()) {
                SearchResultEntry se = (SearchResultEntry) cursor.get();
                Entry entry = se.getEntry();
                System.out.println("sn: " + entry.get("sn").getString());

        }
    }

Re: How to do a sorted search using Apache Directory API

Posted by Tamsin Slinn <ta...@gmail.com>.
Hi,

Just to close this off in case anyone else has a similar problem, I found
that if, after installing the overlay, I also specify a Matching Rule ID
for the sort key, I get the ordering I want

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

Cheers,
Tamsin

On Thu, Mar 7, 2019 at 9:36 PM Tamsin Slinn <ta...@gmail.com> wrote:

> Hi
> Thanks for the reply.
> I tried with both 1.0.2 and 2.0.0.AM2 with the same outcome.
> The output from the Java code was as below (I just have a local ldap with
> a few users for testing)
>
> sn: Trainee
> sn: Admin
> sn: User
> sn: Supervisor
>
> I did not know about the slapo-sssvlv overlay, my LDAP knowledge is basic
> - I am running OpenLDAP from the docker image openfrontier/openldap-server
> I tried to install it - I successfully loaded the ldif below. Now when I
> run the original code I get no results at all. If I remove the SortRequest
> control from the SearchRequest it returns me the expected results.
>
> > 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
>
> Is this the correct way to install the slapo-sssvlv overlay? I am not sure
> how to provide it's configuration sorry.
>
> Thanks
>
>
>
>
> On Thu, Mar 7, 2019 at 4:08 PM Emmanuel Lécharny <el...@gmail.com>
> wrote:
>
>>
>> On 07/03/2019 10:40, Tamsin Slinn wrote:
>> > Hi,
>> > I hope this is an OK question to post to the list.
>>
>>
>> That's fine.
>>
>>
>> >
>> > I'm running the code below against OpenLDAP 2.4.40. I based the code on
>> an
>> > integration test for SortRequestControl. However, this code does not
>> return
>> > the entries in the order I expect (alphabetical by sn). Can you tell me
>> > what I am missing?
>> > Thanks for any help!
>> >
>> >      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 (SearchCursor cursor = connection.search(req)) {
>> >           while (cursor.next()) {
>> >                  SearchResultEntry se = (SearchResultEntry)
>> cursor.get();
>> >                  Entry entry = se.getEntry();
>> >                  System.out.println("sn: " +
>> entry.get("sn").getString());
>> >
>> >          }
>> >      }
>>
>> First, which version of the API are you using ?
>>
>>
>> Then, can you provide the result you get ?
>>
>>
>> Last, not least, are you sure your OpenLDAP server has the slapo-sssvlv
>> overlay installed ?
>>
>> If so, can you provide its configuration ?
>>
>>
>>

Re: How to do a sorted search using Apache Directory API

Posted by Tamsin Slinn <ta...@gmail.com>.
Hi
Thanks for the reply.
I tried with both 1.0.2 and 2.0.0.AM2 with the same outcome.
The output from the Java code was as below (I just have a local ldap with a
few users for testing)

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

I did not know about the slapo-sssvlv overlay, my LDAP knowledge is basic -
I am running OpenLDAP from the docker image openfrontier/openldap-server
I tried to install it - I successfully loaded the ldif below. Now when I
run the original code I get no results at all. If I remove the SortRequest
control from the SearchRequest it returns me the expected results.

> 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

Is this the correct way to install the slapo-sssvlv overlay? I am not sure
how to provide it's configuration sorry.

Thanks




On Thu, Mar 7, 2019 at 4:08 PM Emmanuel Lécharny <el...@gmail.com>
wrote:

>
> On 07/03/2019 10:40, Tamsin Slinn wrote:
> > Hi,
> > I hope this is an OK question to post to the list.
>
>
> That's fine.
>
>
> >
> > I'm running the code below against OpenLDAP 2.4.40. I based the code on
> an
> > integration test for SortRequestControl. However, this code does not
> return
> > the entries in the order I expect (alphabetical by sn). Can you tell me
> > what I am missing?
> > Thanks for any help!
> >
> >      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 (SearchCursor cursor = connection.search(req)) {
> >           while (cursor.next()) {
> >                  SearchResultEntry se = (SearchResultEntry) cursor.get();
> >                  Entry entry = se.getEntry();
> >                  System.out.println("sn: " +
> entry.get("sn").getString());
> >
> >          }
> >      }
>
> First, which version of the API are you using ?
>
>
> Then, can you provide the result you get ?
>
>
> Last, not least, are you sure your OpenLDAP server has the slapo-sssvlv
> overlay installed ?
>
> If so, can you provide its configuration ?
>
>
>

Re: How to do a sorted search using Apache Directory API

Posted by Emmanuel Lécharny <el...@gmail.com>.
On 07/03/2019 10:40, Tamsin Slinn wrote:
> Hi,
> I hope this is an OK question to post to the list.


That's fine.


>
> I'm running the code below against OpenLDAP 2.4.40. I based the code on an
> integration test for SortRequestControl. However, this code does not return
> the entries in the order I expect (alphabetical by sn). Can you tell me
> what I am missing?
> Thanks for any help!
>
>      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 (SearchCursor cursor = connection.search(req)) {
>           while (cursor.next()) {
>                  SearchResultEntry se = (SearchResultEntry) cursor.get();
>                  Entry entry = se.getEntry();
>                  System.out.println("sn: " + entry.get("sn").getString());
>
>          }
>      }

First, which version of the API are you using ?


Then, can you provide the result you get ?


Last, not least, are you sure your OpenLDAP server has the slapo-sssvlv 
overlay installed ?

If so, can you provide its configuration ?