use of org.elasticsearch.common.geo.GeoUtils in project elasticsearch by elastic.
the class GeoContextMapping method toInternalQueryContexts.
/**
* Parse a list of {@link GeoQueryContext}
* using <code>parser</code>. A QueryContexts accepts one of the following forms:
*
* <ul>
* <li>Object: GeoQueryContext</li>
* <li>String: GeoQueryContext value with boost=1 precision=PRECISION neighbours=[PRECISION]</li>
* <li>Array: <pre>[GeoQueryContext, ..]</pre></li>
* </ul>
*
* A GeoQueryContext has one of the following forms:
* <ul>
* <li>Object:
* <ul>
* <li><pre>GEO POINT</pre></li>
* <li><pre>{"lat": <i><double></i>, "lon": <i><double></i>, "precision": <i><int></i>, "neighbours": <i><[int, ..]></i>}</pre></li>
* <li><pre>{"context": <i><string></i>, "boost": <i><int></i>, "precision": <i><int></i>, "neighbours": <i><[int, ..]></i>}</pre></li>
* <li><pre>{"context": <i><GEO POINT></i>, "boost": <i><int></i>, "precision": <i><int></i>, "neighbours": <i><[int, ..]></i>}</pre></li>
* </ul>
* <li>String: <pre>GEO POINT</pre></li>
* </ul>
* see {@link GeoUtils#parseGeoPoint(String, GeoPoint)} for GEO POINT
*/
@Override
public List<InternalQueryContext> toInternalQueryContexts(List<GeoQueryContext> queryContexts) {
List<InternalQueryContext> internalQueryContextList = new ArrayList<>();
for (GeoQueryContext queryContext : queryContexts) {
int minPrecision = Math.min(this.precision, queryContext.getPrecision());
GeoPoint point = queryContext.getGeoPoint();
final Collection<String> locations = new HashSet<>();
String geoHash = stringEncode(point.getLon(), point.getLat(), minPrecision);
locations.add(geoHash);
if (queryContext.getNeighbours().isEmpty() && geoHash.length() == this.precision) {
addNeighbors(geoHash, locations);
} else if (queryContext.getNeighbours().isEmpty() == false) {
queryContext.getNeighbours().stream().filter(neighbourPrecision -> neighbourPrecision < geoHash.length()).forEach(neighbourPrecision -> {
String truncatedGeoHash = geoHash.substring(0, neighbourPrecision);
locations.add(truncatedGeoHash);
addNeighbors(truncatedGeoHash, locations);
});
}
internalQueryContextList.addAll(locations.stream().map(location -> new InternalQueryContext(location, queryContext.getBoost(), location.length() < this.precision)).collect(Collectors.toList()));
}
return internalQueryContextList;
}
Aggregations