The templates project provides a framework for accepting some number of words (or terms) and producing search results from an LDAP. The most common use case being free form search input, like one would expect for a search engine.

A SearchTemplatesOperation is configured with an ordered number of SearchTemplates. The first SearchTemplates object is expected to format search filters for one term queries. The second SearchTemplates object is expected to format search filters for two term queries, and so forth. The appropriate SearchTemplates is selected based on the Query that is provided to the operation and a search is executed for each of the filters in that templates. A single SearchResponse containing all results is returned. In this fashion a set of search terms can be transformed into LDAP search results.

Some named parameters are defined by the templates in order to write search filters:

SearchTemplates oneTermTemplate = new SearchTemplates(

SearchTemplates twoTermTemplate = new SearchTemplates(
  "(cn={term1} {term2})",
  "(cn={term1}* {term2}*)",
  "(cn=*{term1}* *{term2}*)");

SearchTemplates threeTermTemplate = new SearchTemplates(
  "(|(cn={term1} {term2} {term3})(cn={term2} {term1} {term3}))",
  "(|(cn={term1}* {term2}* {term3}*)(cn={term2}* {term1}* {term3}*))",
  "(|(cn=*{term1}* *{term2}* *{term3}*)(cn=*{term2}* *{term1}* *{term3}*))",

// create a pooled connection factory for searching
PooledConnectionFactory cf = new PooledConnectionFactory("ldap://directory.ldaptive.org");

SearchTemplatesOperation search = new SearchTemplatesOperation(
  new SearchOperationWorker(new SearchOperation(cf)),

// get results for a one term query
Query oneTermQuery = new Query("fisher");
SearchResponse oneTermResult = search.execute(oneTermQuery);

// get results for a two term query
Query twoTermQuery = new Query("daniel fisher");
SearchResponse twoTermResult = search.execute(twoTermQuery);

// get results for a three term query
Query threeTermQuery = new Query("daniel william fisher");
SearchResponse threeTermResult = search.execute(threeTermQuery);

Templates support is provided in a separate library that is available in the jars directory of the latest download.

Or included as a maven dependency: