A newer version of this documentation is available.

View Latest

Creating a Query: Radius-Based

      +

      This section and those following, provide examples of the query-bodies required to make geospatial queries with the Couchbase REST API. Note that more detailed information on performing queries with the Couchbase REST API can be found in Searching with the REST API; which shows how to use the full curl command and how to incorporate query-bodies into it.

      The following query-body specifies a longitude of -2.235143 and a latitude of 53.482358. The target-field geo is specified, as is a distance of 100 miles: this is the radius within which target-locations must reside for their documents to be returned.

      {
        "from": 0,
        "size": 10,
        "query": {
          "location": {
            "lon": -2.235143,
            "lat": 53.482358
           },
            "distance": "100mi",
            "field": "geo"
          },
        "sort": [
          {
            "by": "geo_distance",
            "field": "geo",
            "unit": "mi",
            "location": {
            "lon": -2.235143,
            "lat": 53.482358
            }
          }
        ]
      }

      The query contains a sort object, which specifies that the returned documents should be ordered in terms of their geo_distance from specified lon and lat coordinates: these values need not be identical to those specified in the query object.

      A subset of formatted console output might appear as follows:

                  .
                  .
                  .
      "hits": [
        {
          "index": "geoIndex_61d8c796ef7f4360_acbbef99",
          "id": "landmark_17411",
          "score": 1.4045076008239446e-06,
          "sort": [
            " \u0001?E#9>N\f\"e"
          ]
        },
        {
          "index": "geoIndex_61d8c796ef7f4360_acbbef99",
          "id": "landmark_17409",
          "score": 1.4045076008239446e-06,
          "sort": [
            " \u0001?O~i*(kD,"
          ]
        },
        {
          "index": "geoIndex_61d8c796ef7f4360_acbbef99",
          "id": "landmark_17403",
          "score": 1.4045076008239446e-06,
          "sort": [
            " \u0001?Sg*|/t\u001f\u0002"
          ]
        },
        {
          "index": "geoIndex_61d8c796ef7f4360_acbbef99",
          "id": "hotel_17413",
          "score": 1.4045076008239446e-06,
          "sort": [
            " \u0001?U]S\\.e\u0002_"
         ]
        },
                  .
                  .
                  .