Create a Search Index with the REST API and curl/HTTP

  • how-to
    +
    You can create a Search index with the Search Service API.

    You must create a Search index before you can run a search with the Search Service.

    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.

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

    • You have installed the Couchbase command-line tool (CLI).

    • You have the hostname or IP address for the node in your database where you’re running the Search Service. For more information about where to find the IP address for your node, see List Cluster Nodes.

    Procedure

    To create a Search index with the REST API:

    1. In your command-line tool, enter a curl command with the XPUT verb.

    2. Set your header content to include "Content-Type: application/json".

    3. Enter your username, password, and the Search Service endpoint on port 8094 with the name of the index you want to create:

      curl -s -XPUT -H "Content-Type: application/json" \
          -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/travel-sample/scope/inventory/index/landmark-content-index 
          -d \

      To use SSL, use the https protocol in the Search Service endpoint URL and port 18094.

      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. Enter the JSON payload for the settings you want in your index.

      Do not include the uuid or sourceUUID parameters.

      If you remove the uuid and sourceUUID parameters, you can copy the Search index definition JSON payload from the Couchbase Server Web Console to use in your REST API call. For more information about how to create an index with the UI, see Create a Basic Search Index with the Web Console.

    Example

    In the following example, the JSON payload creates a simple index named landmark-content-index on the travel-sample bucket. It creates a type mapping for the inventory.landmark collection, with a child field, content, and adds a dynamic Extended Attributes (XATTRs) mapping for any available document metadata fields in the collection:

    curl -s -XPUT -H "Content-Type: application/json" \
      -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/travel-sample/scope/inventory/index/landmark-content-index 
      -d \
      '{
          "type": "fulltext-index",
          "name": "landmark-content-index",
          "sourceType": "gocbcore",
          "sourceName": "travel-sample",
          "planParams": {
            "maxPartitionsPerPIndex": 1024,
            "indexPartitions": 1
          },
          "params": {
            "doc_config": {
              "docid_prefix_delim": "",
              "docid_regexp": "",
              "mode": "scope.collection.type_field",
              "type_field": "type"
            },
            "mapping": {
              "analysis": {},
              "default_analyzer": "standard",
              "default_datetime_parser": "dateTimeOptional",
              "default_field": "_all",
              "default_mapping": {
                "dynamic": true,
                "enabled": false
              },
              "default_type": "_default",
              "docvalues_dynamic": false,
              "index_dynamic": true,
              "store_dynamic": false,
              "type_field": "_type",
              "types": {
                "inventory.landmark": {
                  "dynamic": false,
                  "enabled": true,
                    "properties": {
                      "_$xattrs": {
                      "dynamic": true,
                      "enabled": true
                    },
                    "content": {
                      "dynamic": false,
                      "enabled": true,
                      "fields": [
                        {
                          "docvalues": true,
                          "include_in_all": true,
                          "include_term_vectors": true,
                          "index": true,
                          "name": "content",
                          "store": true,
                          "type": "text"
                        }
                      ]
                    }
                  }
                }
              }
            },
            "store": {
              "indexType": "scorch",
              "segmentVersion": 15
            }
          },
          "sourceParams": {}
        }'
    XATTRs mappings are only available in Couchbase Server version 7.6.2 and later.

    For more information about the available JSON properties for a Search index, see Search Index JSON Properties.

    Next Steps

    After you create a Search index, you can Run a Simple Search with the REST API and curl/HTTP to test your Search index.

    If you want to edit your index with another REST API call, include the uuid parameter with the UUID the Search Service assigned to your Search index.

    You can also Run A Simple Search with the Web Console or with one of the Couchbase SDKs: