Search code examples
javaldap

How to use like operator within LDAP query?


I would like to use Like operator within my LDAP query and find any name starting with the String Name. (something like [Name = Mike*]) but apparently it doesn't work. Any help will be appreciated.

public List<String> findByName(String Name) {
    LdapQuery query = query().where("objectclass").is("top")
            .and("Name").like(Name);

    return ldapTemplate.search(query, new NameAttributesMapper());

}

Solution

  • The documentation for LdapQueryBuilder and LikeFilter states that you have to use the * explicitly in the query, so if you're looking for a user starting with Mike you have to use Mike* in the filter syntax; it's just a shorthand to allow you to specify a wildcard without needing to escape it.

    So:

    public List<String> findByName(String Name) {
    LdapQuery query = query().where("objectclass").is("top")
            .and("Name").like(Name + "*");
    
    return ldapTemplate.search(query, new NameAttributesMapper());
    }