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.
Prerequisites
-
You have the Search Service enabled on a node in your database.
-
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 your database.
-
You’ve 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 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/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.
You can copy the JSON for a Query Request from the Couchbase Server Web Console to use in your REST API call. For more information about how to perform a search with the UI, see Run A Simple Search with the Web Console. In the following example, the JSON payload queries an index named
landmark-content-index
for the stringsview
,food
, andbeach
:curl -XPOST -H "Content-Type: application/json" \ -u $CB_USERNAME:$CB_PASSWORD http://$CB_HOSTNAME:8094/api/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 don’t get the search results you were expecting, you can change the JSON payload for your Search index or for your Search query.
You can also Customize a Search Index with the Web Console.