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
-
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.
-
Your user account has the Search Admin or Search Reader role.
-
You 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.
-
You have created a Search index.
For more information about how to create a Search index, see Create a Basic Search Index with the Web Console or Create a Search Index with the REST API and curl/HTTP.
Procedure
To run a simple search with the REST API:
-
In your command-line tool, enter a
curl
command with theXPOST
verb. -
Set your header content to include
"Content-Type: application/json"
. -
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 port18094
. -
Enter the JSON payload for your query.
Couchbase Server version 7.6.2You 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.
You can also Customize a Search Index with the Web Console.