names.remove("");
// reuse a single Document and field instances
Document doc = new Document();
doc.add(new StoredField(GEONAME.key(), fullAncestry ? geoName.getGazetteerRecordWithAncestry() : geoName.getGazetteerRecord()));
doc.add(new IntField(GEONAME_ID.key(), geoName.getGeonameID(), Field.Store.YES));
// if the alternate names file was loaded and we found a preferred name for this GeoName, store it
if (preferredName != null) {
doc.add(new StoredField(PREFERRED_NAME.key(), preferredName.name));
}
// index the direct parent ID in the PARENT_ID field
GeoName parent = geoName.getParent();
if (parent != null) {
doc.add(new IntField(PARENT_ID.key(), parent.getGeonameID(), Field.Store.YES));
}
// index all ancestor IDs in the ANCESTOR_IDS field; this is a secondary field
// so it can be used to restrict searches and PARENT_ID can be used for ancestor
// resolution
while (parent != null) {
doc.add(new IntField(ANCESTOR_IDS.key(), parent.getGeonameID(), Field.Store.YES));
parent = parent.getParent();
}
doc.add(new LongField(POPULATION.key(), geoName.getPopulation(), Field.Store.YES));
// set up sort field based on population and geographic feature type
if (geoName.getFeatureClass().equals(FeatureClass.P) || geoName.getFeatureCode().name().startsWith("PCL")) {
if (geoName.getGeonameID() != 2643741) // todo: temporary hack until GeoNames.org fixes the population for City of London
// boost cities and countries when sorting results by population
doc.add(new LongField(SORT_POP.key(), geoName.getPopulation() * 11, Field.Store.YES));
} else {
// don't boost anything else, because people rarely talk about other stuff
// (e.g., Washington State's population is more than 10x that of Washington, DC
// but Washington, DC is mentioned far more frequently than Washington State)
doc.add(new LongField(SORT_POP.key(), geoName.getPopulation(), Field.Store.YES));
}
doc.add(new IntField(HISTORICAL.key(), IndexField.getBooleanIndexValue(geoName.getFeatureCode().isHistorical()), Field.Store.NO));
doc.add(new StringField(FEATURE_CODE.key(), geoName.getFeatureCode().name(), Field.Store.NO));
// create a unique Document for each name of this GeoName
TextField nameField = new TextField(INDEX_NAME.key(), "", Field.Store.YES);
doc.add(nameField);