Run a Simple Search with the REST API and curl/HTTP

  • how-to
    +
    You can use the REST API and a curl command to run a search against a Search index.

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

    Prerequisites

    Procedure

    To run a simple search with the REST API:

    1. In your command-line tool, enter a curl command with the XPOST 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 query:

      curl -s -XPUT -H "Content-Type: application/json" \
          -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/${BUCKET-NAME}/scope/${SCOPE-NAME}/index/${INDEX-NAME}/query -d \

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

    4. Enter the JSON payload for your query.

      Couchbase Server version 7.6.2

      You can copy the JSON for a Search query from the Couchbase Server Web Console to use in your REST API call. You can choose to copy a full command-line curl example, or copy just the JSON payload. For more information about how to perform a search with the UI, see Run A Simple Search with the Web Console.

    Example

    In the following example, the JSON payload queries an index named landmark-content-index for the strings view, food, and beach:

    curl -XPOST -H "Content-Type: application/json" \
      -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/bucket/travel-sample/scope/inventory/index/landmark-content-index/query \
      -d '{
        "explain": true,
        "fields": [
          "*"
        ],
        "highlight": {},
        "query": {
          "query": "+view +food +beach"
        },
        "size": 10,
        "from": 0
      }'

    For more information about the available properties for a Search query JSON payload, see Search Request JSON Properties.

    Next Steps

    If you do not get the search results you were expecting, you can change the JSON payload for your Search index or for your Search query.

    If your database is running Couchbase Server version 7.6.2 and later, you can also copy a full command-line curl example from the Server Web Console to use with the REST API, or edit a generated Search query with the built-in code editor.