QueryResult
QueryResult
A QueryResult holds the response of one find request: the matching entries, and the count and content type when you asked for them. Use this class to
- read the entries that matched a query
- read the total number of matching entries
- read the content type of the entries
You do not create a QueryResult yourself. The SDK builds one from the response and passes it to the onCompletion(ResponseType responseType, QueryResult queryresult, Error error) method of your QueryResultsCallBack. See the Callbacks class page. When error is not null, queryresult is null.
getContentType
getContentType returns the content type of the queried entries.
Returns: JSONObject. The content type of the queried entries.
Validation
- There is no client-side validation. getContentType reads a value the SDK filled when the response arrived.
Additional Resource Get a single content type describes the content type object.
Behavior
- Client-side only. getContentType makes no request.
- The SDK fills the value only when the query used the includeContentType method. Without it, getContentType returns null.
- The object is the content_type key of the response, with nested objects and arrays as JSONObject and JSONArray.
- The getContentType method of Query is a different method. It returns the content type UID as a String.
Example
Read the content type title with the entries
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
import org.json.JSONObject;
public class ReadContentType {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.includeContentType();
query.find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
JSONObject contentType = queryResult.getContentType();
if (contentType != null) {
System.out.println(contentType.optString("title") + " " + contentType.optString("uid"));
}
}
});
}
}getCount
getCount returns the number of entries that matched the query.
Returns: int. The number of matching entries.
Validation
- There is no client-side validation. getCount reads a value the SDK filled when the response arrived.
Additional Resource The Count request describes the include_count and count parameters.
Behavior
- Client-side only. getCount makes no request.
- The SDK fills the value only when the query used the includeCount or the count method. Without either, getCount returns 0.
- A 0 therefore means either no match or no count requested. Call includeCount to tell them apart.
- The SDK reads the count key of the response. When that is missing or 0, it reads the entries key if that key holds a number.
- The number covers every matching entry, not only the page that limit and skip select.
Example
Read the total and the page size
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
public class ReadTotal {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.includeCount().limit(10);
query.find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
System.out.println("total: " + queryResult.getCount());
System.out.println("on this page: " + queryResult.getResultObjects().size());
}
});
}
}Read only the count
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
public class ReadCountOnly {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.where("category", "news").count();
query.find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
if (error != null) { System.out.println(error.getErrorMessage()); return; }
System.out.println("news entries: " + queryResult.getCount());
}
});
}
}getResultObjects
getResultObjects returns the entries that matched the query.
Returns: List<Entry>. The matching entries.
Validation
- There is no client-side validation. getResultObjects reads a value the SDK filled when the response arrived.
- QueryResult exists only on success. When the callback receives an Error, queryresult is null, so check error first.
Additional Resource Get all entries describes the response that fills this list.
Behavior
- Client-side only. getResultObjects makes no request.
- On success the list is never null. It is empty when no entry matches, or when the query used the count method.
- Each Entry carries the UID, title, URL, tags and the full entry JSON. Read fields with get, getString or toJSON. See the Entry class page.
- getLocale() returns null on these entries, because the SDK does not copy the locale from the response. Read it with entry.getString("locale").
- The list holds at most one page. Without limit, the API returns its default of 100 entries. Combine skip, limit and includeCount to read the rest, as Pagination on the Query class page describes.
Example
Print the UID, title and locale of each entry
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
public class ListEntries {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
for (Entry entry : queryResult.getResultObjects()) {
System.out.println(entry.getUid() + " " + entry.getTitle() + " " + entry.getString("locale"));
}
}
});
}
}getSchema
getSchema returns the schema array from the top level of the response.
Returns: JSONArray. The schema array from the response.
Validation
- There is no client-side validation. getSchema reads a value the SDK filled when the response arrived.
- Known issue: In 2.8.3, getSchema returns null for every response. To read the schema, call the includeContentType method on the query, then read queryResult.getContentType().optJSONArray("schema").
Additional Resource Get all entries describes the response the SDK reads.
Behavior
- Client-side only. getSchema makes no request.
- No Query method asks for a top-level schema key. The includeContentType method deletes an include_schema parameter set through addQuery or addParam.
- The getContentType method returns the content type, which holds the schema.
Example
Read the schema through the content type
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Query;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReadSchema {
public static void main(String[] args) throws IllegalAccessException {
Stack stack = Contentstack.stack("<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>");
Query query = stack.contentType("<CONTENT_TYPE_UID>").query();
query.includeContentType();
query.find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
if (error != null) {
// The API rejected the request, or it did not complete
System.out.println("Request failed: " + error.getErrorCode() + " " + error.getErrorMessage());
return;
}
// getSchema() returns null in 2.8.3, so read the schema from the content type
JSONObject contentType = queryResult.getContentType();
JSONArray schema = contentType == null ? null : contentType.optJSONArray("schema");
System.out.println(schema);
}
});
}
}