Cloudant Query provides you with a declarative way to define and query indexes. If you’re new to Cloudant, this is the easiest way to start querying your database.
Use Cloudant Query
See how to build and query data using Cloudant Query.
Before starting the tutorial, if you haven’t created a Cloudant account yet, complete the steps on the Get Started page.
Tutorial: Build and query using Cloudant Query
This tutorial shows you how to build and query your data using Cloudant Query from the dashboard. ▼More
Replicate and view the sample database
You’ll be working with a sample database in this tutorial. Follow these steps to replicate the sample database.
From the Cloudant dashboard, replicate the https://examples.cloudant.com/movies-demo remote database into your account. If you need help creating the replication request, refer to the Replication video and Tutorial on the Replicate a Sample Database tutorial.
After replicating the movies-demo database, in a browser, access the database to see a list of documents. https://<account>.cloudant.com/movies-demo/_all_docs?limit=20&include_docs=true
You can also see a list of indexes defined in the database. https://<account>.cloudant.com/movies-demo/_index
Create an index
Next, you’ll see how to create an index using Cloudant Query syntax. Follow these steps to create the index.
Next, open the database.
Next to Query, click New Query Index.
Paste the following JSON text into the Index text box, then click Create Index.
{
“index” : {
“fields”: [
“Person_dob”
]
},
“name”:”age-index”,
“type”:”json”
}
You can automatically index all fields using this syntax: { "index": {}, "type": "text" }
Next to Cloudant Query, click left arrow to return to the All Documents view.
Query an index
Now you’re ready to query the index. Follow these steps to build some queries.
Click Query from the menu.
Paste this JSON text into the Cloudant Query text field, and click Run Query. This example finds all of the movies with Robert De Niro with any release year.
{
“selector” : {
“Person_name”: “Robert De Niro”,
“Movie_year”: {“$gt”: 0}
}
}
Run a new query using the following JSON text. This example sorts by the year released, limits the results to the first 10 movies, and skips the first movie.
{
“selector” : {
“Person_name”: “Robert De Niro”,
“Movie_year”: {“$gt”: 0}
},
“sort”: [“Movie_year”],
“limit”: 10,
“skip”: 1
}
Run a new query using the following JSON text. This example finds all of the movies with Robert De Niro with any release year. and returns the movie name and year released sorted by movie year.
{
“selector” : {
“Person_name”: “Robert De Niro”,
“Movie_year”: {“$gt”: 0}
},
“fields”: [“Movie_name”, “Movie_year”],
“sort”: [“Movie_year”],
}
If the fields list is omitted, the entire document is returned.
Run a new query using the following JSON text. This example finds all De Niro films from a specific year, 1978.
{
“selector” : {
“Person_name”: “Robert De Niro”,
“Movie_year”: 1978
}
}
Run a new query using the following JSON text.
{
“selector” : {
“Movie_earnings_rank”: 191
}
}
You should see an error indicating “There is no index available for this selector”. If the user submits a query that does not have a suitable index, then it is up to the developer to decide at the application layer how such errors are handled. Cloudant does not automatically re-index the whole data set.
Run a new query using the following JSON text. This example selects Robert De Niro movies and sorts in ascending order of the person’s name with a secondary sort on the year the movie was released.
{
“selector” : {
“Person_name”: “Robert De Niro”
},
“sort”: [
{“Person_name”:”asc”},
{“Movie_year”:”asc”}
]
}
For the “text” type, you need to append :number or :string to the sort field as shown below.