Run a Geospatial Search Query with the Web Console

  • how-to
    +
    Search for geospatial data in your Couchbase Server database with a compatible Search index and the Couchbase Server Web Console.

    For more information about how the Search Service scores documents in search results, see Scoring for Search Queries.

    Prerequisites

    • You have the Search Service enabled on a node in your database. For more information about how to deploy a new node and Services on your database, see Manage Nodes and Clusters.

    • You have a bucket with scopes and collections in your database. For more information about how to create a bucket, see Create a Bucket.

    • You have documents in your database that contain geospatial data.

    • Your user account has the Search Admin role for the bucket where you want to create the Search index.

      If you only want to run a search, you only need the Search Reader role.

    • You have logged in to the Couchbase Server Web Console.

    Procedure

    To run a geospatial Search query, create a Search index with a geospatial type mapping.

    Create a Search Index with a Geospatial Type Mapping

    To create the Search index from the Web Console:

    1. Go to Search.

    2. Click Add Index.

    3. In the Index Name field, enter a name for the index.

      Your index name must start with an alphabetic character (a-z or A-Z). It can only contain alphanumeric characters (a-z, A-Z, or 0-9), hyphens (-), or underscores (_).

      For Couchbase Server version 7.6 and later, your index name must be unique inside your selected bucket and scope. You cannot have 2 indexes with the same name inside the same bucket and scope.

    4. In the Bucket list, select the bucket where you want to create the index.

    5. Expand Customize Index.

    6. Select Use non-default scope/collection(s).

      1. In the Scope list, select the scope that contains the documents you want to include in your index.

    7. Create a Type Mapping on the collection in your database that you want to search.

    8. Create a Child Field on the new type mapping with the following settings:

      1. In the Field field, enter the name of the field in your documents that contains the geospatial data you want to search.

      2. In the Type list, select geopoint.

      3. Select Index.

      4. Select Include in _all field.

    9. Click OK.

    10. Click Create Index.

    Run a Geospatial Search Query

    To run a Search query against the Search index from the Web Console:

    1. Go to Search.

    2. Click the index where you want to run a search.

    3. In the Search this index field, enter a search query.

      For example, the following query searches a geospatial field, geo, for any locations within a 100 mile radius of the coordinates -2.235143, 53.482358:

      {
          "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
              }
            }
          ]
      }
    Couchbase Server version 7.6.2

    You can generate a full command-line curl example from the preceding query in the Server Web Console to use with the REST API. Click show advanced query settings, then show command-line curl example to get the example code.

    Next Steps

    You can customize your Search index to improve search results and performance.

    If you want to add autocomplete to your database’s search, see Use Autocomplete with the Search Service.