// 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);
// see if anything was found with fuzzy matching
if (results.scoreDocs.length > 0) {
// one or more fuzzy 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, true);
logger.debug(location + "{fuzzy}");
candidateMatches.add(location);
}
} else {
// drats, foiled again! no fuzzy matches found either!