in baremaps-server/src/main/java/org/apache/baremaps/server/GeocoderResource.java [65:104]
public Response searchLocations(
@QueryParam("queryText") String queryText,
@QueryParam("countryCode") @DefaultValue("") String countryCode,
@QueryParam("limit") @DefaultValue("10") int limit) {
if (queryText == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
.entity("The queryText parameter is mandatory").build());
}
try {
IndexSearcher searcher = searcherManager.acquire();
try {
// Querying to search location uses AND operator between terms such as every term "adds up"
// Examples of queryText:
// - "paris", returns paris in france in first results (i.e because of scoring with
// population)
// - "paris brazil", returns paris in brazil and not paris in france.
var query = new GeonamesQueryBuilder()
.queryText(queryText).countryCode(countryCode).scoringByPopulation()
.andOperator()
.build();
var result = searcher.search(query, limit);
var results =
Arrays.stream(result.scoreDocs).map(scoreDoc -> asResult(searcher, scoreDoc)).toList();
return Response.status(Response.Status.OK).header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.header(CONTENT_TYPE, APPLICATION_JSON).entity(new GeocoderResponse(results)).build();
} catch (IllegalArgumentException e) {
logger.warn("Illegal input while processing request", e);
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (IOException | ParseException e) {
logger.error("Error while processing request", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} finally {
searcherManager.release(searcher);
}
} catch (IOException e) {
logger.error("Error while processing request", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}