W3cubDocs

/RethinkDB Java

ReQL command: getNearest

Command syntax

table.getNearest(point).optArg("index", index) → array

Description

Return a list of documents closest to a specified point based on a geospatial index, sorted in order of increasing distance.

The index optArg is mandatory. Optional arguments are:

  • max_results: the maximum number of results to return (default 100).
  • unit: Unit for the distance. Possible values are m (meter, the default), km (kilometer), mi (international mile), nm (nautical mile), ft (international foot).
  • max_dist: the maximum distance from an object to the specified point (default 100 km).
  • geo_system: the reference ellipsoid to use for geographic coordinates. Possible values are WGS84 (the default), a common standard for Earth’s geometry, or unit_sphere, a perfect sphere of 1 meter radius.

The return value will be an array of two-item objects with the keys dist and doc, set to the distance between the specified point and the document (in the units specified with unit, defaulting to meters) and the document itself, respectively. The array will be sorted by the values of dist.

Example: Return a list of the closest 25 enemy hideouts to the secret base.

import com.rethinkdb.gen.ast.Point;

Point secretBase = r.point(-122.422876,37.777128);

r.table("hideouts")
 .getNearest(secretBase)
 .optArg("index", "location")
 .optArg("max_results", 25)
 .run(conn);

If you wish to find all points within a certain radius of another point, it’s often faster to use getIntersecting with circle, as long as the approximation of a circle that circle generates is sufficient.

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/java/get_nearest/