Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9.8 Querying Views
Chapter Sections
Chapters

9.8.1. Querying Using the REST API

Querying can be performed through the REST API endpoint. The REST API supports and operates using the core HTTP protocol, and this is the same system used by the client libraries to obtain the view data.

Using the REST API you can query a view by accessing any node within the Couchbase Server cluster on port 8092. For example:

HTTP Request
GET http://localhost:8092/bucketname/_design/designdocname/_view/viewname

Where:

When accessing a view stored within an SASL password-protected bucket, you must include the bucket name and bucket password within the URL of the request:

HTTP Request
GET http://bucketname:password@localhost:8092/bucketname/_design/designdocname/_view/viewname

Additional arguments to the URL request can be used to select information from the view, and provide limit, sorting and other options. For example, to output only ten items:

HTTP Request
GET http://localhost:8092/bucketname/_design/designdocname/_view/viewname?limit=10

The formatting of the URL follows the HTTP specification. The first argument should be separated from the base URL using a question mark (?). Additional arguments should be separated using an ampersand (&). Special characters should be quoted or escaped according to the HTTP standard rules.

The additional supported arguments are detailed in the table below.

MethodGET /bucket/_design/design-doc/_view/view-name
Request DataNone
Response DataJSON of the rows returned by the view
Authentication Requiredno
Query Arguments 
descendingReturn the documents in descending by key order
 Parameters: boolean; optional
endkeyStop returning records when the specified key is reached. Key must be specified as a JSON value.
 Parameters: string; optional
endkey_docidStop returning records when the specified document ID is reached
 Parameters: string; optional
full_setUse the full cluster data set (development views only).
 Parameters: boolean; optional
groupGroup the results using the reduce function to a group or single row
 Parameters: boolean; optional
group_levelSpecify the group level to be used
 Parameters: numeric; optional
inclusive_endSpecifies whether the specified end key should be included in the result
 Parameters: boolean; optional
keyReturn only documents that match the specified key. Key must be specified as a JSON value.
 Parameters: string; optional
keysReturn only documents that match each of keys specified within the given array. Key must be specified as a JSON value. Sorting is not applied when using this option.
 Parameters: array; optional
limitLimit the number of the returned documents to the specified number
 Parameters: numeric; optional
on_errorSets the response in the event of an error
 Parameters: string; optional
Supported Values 
 continue: Continue to generate view information in the event of an error, including the error information in the view response stream.
 stop: Stop immediately when an error condition occurs. No further view information will be returned.
reduceUse the reduction function
 Parameters: boolean; optional
skipSkip this number of records before starting to return the results
 Parameters: numeric; optional
staleAllow the results from a stale view to be used
 Parameters
 Supported Values: string; optional
 false: Force a view update before returning data
 ok: Allow stale views
 update_after: Allow stale view, update view after it has been accessed
startkeyReturn records with a value equal to or greater than the specified key. Key must be specified as a JSON value.
 Parameters: string; optional
startkey_docidReturn records starting with the specified document ID
 Parameters: string; optional

The output from a view will be a JSON structure containing information about the number of rows in the view, and the individual view information.

An example of the View result is shown below:

JSON
{
  "total_rows": 576,
  "rows" : [
      {"value" : 13000, "id" : "James", "key" : ["James", "Paris"] },
      {"value" : 20000, "id" : "James", "key" : ["James", "Tokyo"] },
      {"value" : 5000,  "id" : "James", "key" : ["James", "Paris"] },

    ]
}

The JSON returned consists of two fields:

In the event of an error, the HTTP response will be an error type (not 200), and a JSON structure will be returned containing two fields, the basic error and a more detailed reason field. For example:

JSON
{
  "error":"bad_request",
  "reason":"invalid UTF-8 JSON: {{error,{1,\"lexical error: invalid char in json text.\\n\"}},\n                     \"Paris\"}"
}

Note

If you supply incorrect parameters to the query, an error message is returned by the server. Within the Client Libraries the precise behavior may differ between individual language implementations, but in all cases, an invalid query should trigger an appropriate error or exception.

Detail on each of the parameters, and specific areas of interaction are described within the additional following sections, which also apply to all client library interfaces.