6.5. Getting Search Results

Depending on the method you use to search, you handle results in different ways.

  • You can get a ConnectionEntryReader, and iterate over the reader to access individual search results.

    Connection connection = ...;
    ConnectionEntryReader reader = connection.search("dc=example,dc=com",
        SearchScope.WHOLE_SUBTREE, "(objectClass=person)");
    try
    {
      while (reader.hasNext())
      {
        if (reader.isEntry())
        {
          SearchResultEntry entry = reader.readEntry();
    
          // Handle entry...
        }
        else
        {
          SearchResultReference ref = reader.readReference();
    
          // Handle continuation reference...
        }
      }
    }
    catch (IOException e)
    {
      // Handle exceptions...
    }
    finally
    {
      reader.close();
    }

    For a complete example in context, see Search.java, one of the OpenDJ LDAP SDK examples.

  • You can pass in a collection of SearchResultEntrys (and optionally a collection of SearchResultReferences) to which the SDK adds the results. For this to work, you need enough memory to hold everything the search returns.

  • You can pass in a SearchResultHandler to manage results.

  • With searchSingleEntry() and readEntry(), you can get a single SearchResultEntry with methods to access the entry content.