// Lucene query used to look for exact match on the "geonameID" field
Query q = NumericRangeQuery.newIntRange(GEONAME_ID.key(), parentId, parentId, true, true);
TopDocs results = indexSearcher.search(q, null, 1, POPULATION_SORT);
if (results.scoreDocs.length > 0) {
Document doc = indexSearcher.doc(results.scoreDocs[0].doc);
GeoName parent = GeoName.parseFromGeoNamesRecord(doc.get(GEONAME.key()), doc.get(PREFERRED_NAME.key()));
parentMap.put(parent.getGeonameID(), parent);
if (!parent.isAncestryResolved()) {
Integer grandParentId = PARENT_ID.getValue(doc);
if (grandParentId != null) {
Set<GeoName> geos = grandParentMap.get(grandParentId);
if (geos == null) {
geos = new HashSet<GeoName>();
grandParentMap.put(grandParentId, geos);
}
geos.add(parent);
}
}
} else {
LOG.error("Unable to find parent GeoName [{}]", parentId);
}
}
// find all parents of the parents
if (!grandParentMap.isEmpty()) {
resolveParents(grandParentMap);
}
// set parents of children
for (Integer parentId : childMap.keySet()) {
GeoName parent = parentMap.get(parentId);
if (parent == null) {
LOG.info("Unable to find parent with ID [{}]", parentId);
continue;
}
for (GeoName child : childMap.get(parentId)) {