Let us start with the simplest example: querying all KiiObjects in a bucket.
Here is what is happening in the sample code:
Creates a KiiQuery instance without a KiiClause instance. This query will get all KiiObjects.
Queries KiiObjects by calling the query() method in the target bucket by passing the query instance. Alternatively, you can call the method with a value of null.
Parses the query results returned as a KiiQueryResult instance by calling the getResult() method.
Executing an "all" query in a bucket will return all KiiObjects in it.
If you cannot get the expected result, check if you are querying the bucket in the correct scope (See here for more discussion).
Querying with multiple conditions
Next, let's query KiiObjects with the following conditions:
The field "gender" has a value of "Female".
The field "age" has a value greater than 18.
Sort the query results in ascending order by the field "age".
Limit the number of query results returned per call to 10.
Here is what is happening in the sample code:
Creates query conditions by calling the equals() and greaterThan() methods. Then creates a KiiClause instance by concatenating these two conditions with the and() method. Finally, the code creates a KiiQuery instance with this KiiClause instance.
Calls the sortByAsc() method of the KiiQuery instance to define the sort order.
Calls the setLimit() method of the KiiQuery instance to define the maximum number of KiiObjects returned in each query result set.
Queries KiiObjects by calling the query() method in the target bucket by passing the query instance.
Parses the query results returned as a KiiQueryResult instance by calling the getResult() method.
In this example, query results are parsed in consideration of pagination:
After parsing through the query results, the hasNext() method of the KiiQueryResult checks if more KiiObjects are available.
If there are more KiiObjects, the getNextQueryResult() method gets a new KiiQueryResult instance that contains the next 10 KiiObjects.
Querying with geolocation data
Let us now see how to query using geolocation data as a query condition.
Suppose that a KiiObject has a field named "location" whose value is a GeoPoint object as shown in the following sample code:
Here is an example that queries KiiObjects within a rectangle area using a GeoBox() clause.
As shown in the sample code, you define a GeoBox() clause by calling the geoBox() method with two GeoPoints, one representing the northeast point, and another one representing the southwest point.
Here is an example of querying KiiObjects within a circular area using a GeoDistance() clause. In this example, we will query KiiObjects that are in the area overlapped by two GeoDistances (GeoDistance1 and GeoDistance2). We will also sort query results in ascending order by the distance from GeoDistance1's center point.
Here is what is happening in the sample code:
Defines two GeoDistance query conditions by calling the geoDistance() method. Here we specify a GeoPoint representing the center, a radius in meters, and optionally a field that stores the distance between the center of the circular area and a returned KiiObject. Kii Cloud will use this field in a query response.
Creates a KiiClause instance by concatenating two GeoDistance query conditions via the and() method. Then creates a KiiQuery instance using this KiiClause instance.
Calls the sortByAsc() method of the KiiQuery instance to set the sort order.
Queries KiiObjects by calling the query() method of the target bucket by passing the query instance.
Parses the query results by calling the getResult() method of the KiiQueryResult instance.
With a GeoDistance query condition, Kii Cloud can return the distance between the center point of a GeoPoint and each returned KiiObject. In this example, the distance between GeoDistance1's center point and KiiObjects are returned.
The distance will be stored in the field specified when the geoDistance() method is called (the "distance_from_center1" field in this example).
To sort query results by distance, call the sortByAsc() method with a string "_calculated." followed by the name of the distance field.
To get the distance, get the value (JSONObject) of the "_calculated" field by calling the getJSONObject() method, and then call the getDouble() method of the distance field.
Querying with predefined keys
Now, let us show an example of querying with predefined keys.
The next sample code gets KiiObjects that are owned by the current user and are either not updated after creation or updated within a day.
Querying with a specific field name and a field type
Now, let us show an example of querying with a specific field name and a field type. KiiObjects can have various custom fields that are not always consistent. By using a hasField() clause, we can narrow the results to KiiObjects that have a specific field of a specific data type.
The next sample code gets KiiObjects that have an optional promotionalCode field.
Querying with a not clause
Now, let us show an example of querying with a not() clause. This time, we will query KiiObjects that are outside a specified rectangle area.
Querying with a not() clause can decrease performance but you might be able to transform the clause to avoid a not operator. See Transforming a not clause for more information.