String sanitizedLocationName = escape(locationName.name.toLowerCase());
try{
// Lucene query used to look for matches based on the
// "indexName" field
Query q = new AnalyzingQueryParser(Version.LUCENE_40,
"indexName", indexAnalyzer).parse("\"" + sanitizedLocationName + "\"");
// collect all the hits up to maxHits, and sort them based
// on Lucene match score and population for the associated
// GeoNames record
TopDocs results = indexSearcher.search(q, null, maxHitDepth, populationSort);
// initialize the return object
List<ResolvedLocation> candidateMatches = new ArrayList<ResolvedLocation>();
// see if anything was found
if (results.scoreDocs.length > 0) {
// one or more exact String matches found for this location name
for (int i = 0; i < results.scoreDocs.length; i++) {
// add each matching location to the list of candidates
ResolvedLocation location = new ResolvedLocation(indexSearcher.doc(results.scoreDocs[i].doc), locationName, false);
logger.debug("{}", location);
candidateMatches.add(location);
}
} else if (fuzzy) { // only if fuzzy matching is turned on
// no exact String matches found -- fallback to fuzzy search
// Using the tilde "~" makes this a fuzzy search. I compared this to FuzzyQuery
// with TopTermsBoostOnlyBooleanQueryRewrite, I like the output better this way.
// With the other method, we failed to match things like "Stra��enhaus Airport"
// as <Stra��enhaus>, and the match scores didn't make as much sense.
q = new AnalyzingQueryParser(Version.LUCENE_40, "indexName", indexAnalyzer).parse(sanitizedLocationName + "~");
// collect all the fuzzy matches up to maxHits, and sort
// them based on Lucene match score and population for the
// associated GeoNames record
results = indexSearcher.search(q, null, maxHitDepth, populationSort);