[389-users] Is there a way to order search results

Andrey Ivanov andrey.ivanov at polytechnique.fr
Thu Dec 24 14:42:21 UTC 2009


Hi,

there is a way to sort the ldap query results. 389 (and some other
LDAP servers) have a functionality called server-side sorting. You can
set a special LDAP control while making your ldap query. This control
has a certain number of parameters, for example, which attributes
should be used for sorting and in which order you want your results to
be sorted. Be careful with the performance implications, be sure that
your search result is small enough.

Some more details are available here :

http://www.redhat.com/docs/manuals/dir-server/8.1/admin/Finding_Directory_Entries-Searching_an_Internationalized_Directory.html
http://www.redhat.com/docs/manuals/dir-server/8.1/admin/Internationalization.html

Just to give you an idea how to do it here is a snippet of a perl
script that makes a search and sorts it :
I think if the server considers that it's too much of a charge  to
make the requested sort it returns you an error message...



use Net::LDAP;
use Net::LDAP::Search;
use Net::LDAP::Control;
use Net::LDAP::Control::Sort;
use Net::LDAP::Constant qw(LDAP_CONTROL_SORTRESULT);

my $ldap = Net::LDAP->new('ldap.example.com', port => 389, version =>
3)  or die "error LDAP: Impossible to contact the server ($@)";
$ldap->bind ( "cn=Directory Manager", password => "secret" );

# Sort on sn and the on givenName with default matching rule
#my $sort_control = Net::LDAP::Control::Sort -> new( order => "sn
givenName", critical => 1 );
#
# Reverse sort on sn with french collation (sorting order matching
rule 'caseIgnoreSubstringMatch -fr')
#my $sort_control = Net::LDAP::Control::Sort -> new( order =>
"-sn:2.16.840.1.113730.3.3.2.18.1.6", critical => 1 );
#
# Reverse sort on sn with default matching rule
my $sort_control = Net::LDAP::Control::Sort -> new( order => "(-sn",
critical => 1 );


my $ldap_filter = "(<your filter returning a sufficiently small set of
entries>)";
my $result_search = $ldap -> search (
                                base    => "dc=example,dc=com",
                                scope   => "sub",
                                filter  => $ldap_filter,
                                attrs   => ['displayName'],
                                control => [ $sort_control ],
                                );
$result_search->code && die $result_search->error;


my ($resp) = $result_search -> control( LDAP_CONTROL_SORTRESULT );
print "The result is sorted\n" if $resp and !$resp->result;

foreach my $ldap_entry ($result_search -> entries())
{
        $ldap_entry -> dump();
}
exit;



@+

2009/12/23 Sean Brady <sbrady at gtfservices.com>:
> Hello,
>
>
>
> Is there a way to have search results returned to the client in a particular
> order?  The application that I am using to query 389 ds is Asterisk, and it
> has some limitations in the way that it handles the search results that are
> returned from the server.  It seems that the order of search results changes
> depending on the attributes present on the object.  I know that there is no
> way that it is 100% consistent, however the attribute order returned by
> searches seems to vary quite a bit.
>
>
>
> Thanks for your help.
>
>
>
> SB
>
>
>
>
>
>
>
> --
> 389 users mailing list
> 389-users at redhat.com
> https://www.redhat.com/mailman/listinfo/fedora-directory-users
>
>




More information about the Fedora-directory-users mailing list