Search code examples
javagoogle-app-enginegoogle-directory-api

Google Directory Service query with space and wildcard: behavior not matching expectations


I ran into some unexpected behavior when trying to combine whitespaces in a Directory Service API search query with a wildcard character:

Code

Directory ds =  new GoogleDirectoryServiceManager().getDirectoryService("admin@randomdomain.com");
Directory.Users usersClient = ds.users();

String lastNameBuffer = term;
StringBuilder sb = new StringBuilder();
if(term.contains(" ")){        
    sb.append("\'");
    sb.append(lastNameBuffer);
    sb.append("*\'");
}else{
    sb.append(lastNameBuffer);
    sb.append("*");
}

queryString = "familyName:" + sb.toString();
users = usersClient.list()
                .setDomain("randomdomain.com")
                .setQuery(queryString)
                .setMaxResults(MAX_DIRECTORY_RESULTS)
                .setFields("users(name,primaryEmail,thumbnailPhotoUrl)")
                .execute();

I added the single quotes to search through the directory with the spaces. Following is an extract from the Google Directory API:

Surround with single quotes ' if the query contains whitespace. Escape single quotes in queries with \', for example 'Valentine\'s Day'.

Input

  1. "johns" --> Returns all users with Johnson name (familyName: johns*) GOOD
  2. "van bur" --> Does not return any users (familyName: 'van bur*') NOT GOOD
  3. "van buren" --> Returns all users with Van Buren name (familyName: 'van buren*') GOOD

tl;dr

Is it not possible to combine a search query with spaces with a wildcard? Or does the wildcard symbol (*) have to be escaped? What query do I need to use to successfully return a list of users in case #2?


Solution

  • During my attempts with the API explorer embedded in the documentation, I have noted that the following query, the same as you without the single quotes, would work as expected on my test domain. Can you test it ?

    familyName:van bur