// matched ancestors in the search results and populate the map;
// otherwise, we cannot populate the map with anything other than
// the best result because we cannot verify which search result
// is the parent of the selected location
if (bestMatch.getGeoname().isAncestryResolved()) {
GeoName parent = bestMatch.getGeoname().getParent();
while (parent != null) {
SearchLevel level = SearchLevel.forGeoName(parent);
if (resultsMap.containsKey(level)) {
// find parent GeoName in the results; this should exist because
// searches are filtered by ancestry from prior results
ResolvedLocation parentLoc = null;
List<ResolvedLocation> searchResults = resultsMap.get(level);
int depth;
for (depth = 0; depth < searchResults.size(); depth++) {
ResolvedLocation loc = searchResults.get(depth);
if (parent.getGeonameID() == loc.getGeoname().getGeonameID()) {
parentLoc = loc;
break;
}
}
if (parentLoc == null) {
// log this as an error condition; it indicates a problem with either
// gazetteer construction or ancestry indexing; this has been noticed
// with certain historical locations, specifically Netherlands Antilles (PCLH),
// which lists Curacao (PCLIX), another country, as a parent. We shouldn't
// fail the entire matching algorithm at this point; just log and ignore
LOG.error(String.format("Missing parent [%s] in search results for match: %s.", parent, bestMatch));
} else {
// found a match, add it to the list
matches.put(level, new Match(level, parentLoc, depth));
}
}
parent = parent.getParent();
}
}
}