// city query should only include city-level feature codes; ancestry is restricted
// to the discovered states or, if no states were found, the discovered countries or,
// if neither states nor countries were found, no ancestry restrictions are added and
// the most populated city will be selected
queryBuilder.location(location.getCity()).clearFeatureCodes().addCityCodes();
if (!states.isEmpty()) {
Set<CountryCode> stateCodes = EnumSet.noneOf(CountryCode.class);
// only clear the parent ID restrictions if states were found; otherwise
// we will continue our search based on the existing country restrictions, if any
queryBuilder.clearParentIds();