Package com.contentstack.sdk
Class Query
java.lang.Object
com.contentstack.sdk.Query
- All Implemented Interfaces:
INotifyClass
public class Query extends Object implements INotifyClass
Contentstack provides certain queries that you can use to fetch
filtered results. You can use queries for Entries and Assets API requests.
-
Field Summary
Fields Modifier and Type Field Description protected ContentType
contentTypeInstance
protected LinkedHashMap<String,Object>
formHeader
protected org.json.JSONObject
urlQueries
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description Query
addParam(String key, String value)
This method adds key and value to an Entry.Query
addQuery(String key, String value)
Add a custom query against specified key.Query
and(ArrayList<Query> queryObjects)
Combines all the queries together using AND operatorQuery
ascending(String key)
Sort the results in ascending order with the given key.Query
containedIn(String key, Object[] values)
Add a constraint to the query that requires a particular key's entry to be contained in the provided array.Query
count()
Retrieve only count of entries in result.Query
descending(String key)
Sort the results in descending order with the given key.Query
except(String[] fieldUids)
Specifies list of field uids that would be 'excluded' from the response.Query
except(ArrayList<String> fieldUid)
Specifies list of field uids that would be 'excluded' from the response.Query
exceptWithReferenceUid(ArrayList<String> fieldUid, String referenceFieldUid)
Specifies an array of 'except' keys that would be 'excluded' in the response.protected void
execQuery(SingleQueryResultCallback callBack, QueryResultsCallBack callback, boolean isFromLocal)
Query
exists(String key)
Add a constraint that requires, a specified key exists in response.Query
find(QueryResultsCallBack callback)
Execute a Query and Caches its result (Optional)Query
findOne(SingleQueryResultCallback callBack)
Execute a Query and Caches its result (Optional)String
getContentType()
void
getResult(Object object, String controller)
void
getResultObject(List<Object> objects, org.json.JSONObject jsonObject, boolean isSingleEntry)
Query
greaterThan(String key, Object value)
Add a constraint to the query that requires a particular key entry to be greater than the provided value.Query
greaterThanOrEqualTo(String key, Object value)
Add a constraint to the query that requires a particular key entry to be greater than or equal to the provided value.Query
includeContentType()
Include Content Type of all returned objects along with objects themselves.Query
includeCount()
Retrieve count and data of objects in resultQuery
includeFallback()
Retrieve the published content of the fallback locale if an entry is not localized in specified localeQuery
includeOwner()
Include object owner's profile in the objects data.Query
includeReference(String key)
Add a constraint that requires a particular reference key details.Query
includeReferenceContentTypUid()
This method also includes the content type UIDs of the referenced entries returned in the responseQuery
language(Language language)
Deprecated.Query
lessThan(String key, Object value)
Add a constraint to the query that requires a particular key entry to be less than the provided value.Query
lessThanOrEqualTo(String key, Object value)
Add a constraint to the query that requires a particular key entry to be less than or equal to the provided value.Query
limit(int number)
A limit on the number of objects to return.Query
locale(String locale)
SetLanguage
instance.Query
notContainedIn(String key, Object[] values)
Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.Query
notEqualTo(String key, Object value)
Add a constraint to the query that requires a particular key's entry to be not equal to the provided value.Query
notExists(String key)
Add a constraint that requires, a specified key does not exists in response.Query
only(String[] fieldUid)
Specifies an array of 'only' keys in BASE object that would be 'included' in the response.Query
onlyWithReferenceUid(ArrayList<String> fieldUid, String referenceFieldUid)
Specifies an array of 'only' keys that would be 'included' in the response.Query
or(ArrayList<Query> queryObjects)
Add a constraint to fetch all entries which satisfy any queries.Query
regex(String key, String regex)
Add a regular expression constraint for finding string values that match the provided regular expression.Query
regex(String key, String regex, String modifiers)
Add a regular expression constraint for finding string values that match the provided regular expression.void
removeHeader(String key)
Remove header key @param key custom_header_keyQuery
removeQuery(String key)
Remove provided query key from custom query if exist.Query
search(String value)
This method provides only the entries matching the specified value.protected void
setContentTypeInstance(ContentType contentTypeInstance)
void
setHeader(String key, String value)
To set headers for Built.io Contentstack rest calls.protected void
setQueryJson(QueryResultsCallBack callback)
Query
skip(int number)
The number of objects to skip before returning any.Query
tags(String[] tags)
Include tags with which to search entries.Query
where(String key, Object value)
Add a constraint to fetch all entries that contains given value against specified keyQuery
whereIn(String key, Query queryObject)
Get entries having values based on referenced fields.Query
whereNotIn(String key, Query queryObject)
Get entries having values based on referenced fields.
-
Field Details
-
contentTypeInstance
-
formHeader
-
urlQueries
protected org.json.JSONObject urlQueries
-
-
Constructor Details
-
Method Details
-
setContentTypeInstance
-
setHeader
To set headers for Built.io Contentstack rest calls.
Scope is limited to this object and followed classes.- Parameters:
key
- header name.value
- header value against given header name.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.setHeader("custom_key", "custom_value");
-
removeHeader
Remove header key @param key custom_header_key- Parameters:
key
-String
Example :
Stack stack = Contentstack..stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.removeHeader("custom_key");
-
getContentType
-
where
Add a constraint to fetch all entries that contains given value against specified key- Parameters:
key
- field uid.value
- field value which get 'included' from the response.- Returns:
Query
object, so you can chain this call.Note : for group field provide key in a "key.groupFieldUid" format.
Example :
Stack stack = Contentstack..stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.where("uid", "bltf4fbsample851db");
-
addQuery
Add a custom query against specified key.- Parameters:
key
- key.value
- value.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack..stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.addQuery("query_param_key", "query_param_value");
-
removeQuery
Remove provided query key from custom query if exist.- Parameters:
key
- Query name to remove.- Returns:
- Query object, so you can chain this call.
Example :
projectQuery.removeQuery("Query_Key");
-
and
Combines all the queries together using AND operator- Parameters:
queryObjects
- list ofQuery
instances on which AND query executes.- Returns:
Query
object, so you can chain this call.
Example ;
Stack stack = Contentstack..stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); Query query = projectClass.query(); query.where('username','something'); Query subQuery = projectClass.query(); subQuery.where('email_address','[email protected]'); ArrayList<Query> array = new ArrayList<Query>();
array.add(query); array.add(subQuery);
projectQuery.and(array);
-
or
Add a constraint to fetch all entries which satisfy any queries.- Parameters:
queryObjects
- list ofQuery
instances on which OR query executes.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); Query query = projectClass.query(); query.where('username','something'); Query subQuery = projectClass.query(); subQuery.where('email_address','[email protected]'); ArrayList<Query> array = new ArrayList<Query>(); array.add(query); array.add(subQuery);
csQuery.or(array);
-
lessThan
Add a constraint to the query that requires a particular key entry to be less than the provided value.- Parameters:
key
- the key to be constrained.value
- the value that provides an upper bound.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.lessThan("due_date", "2013-06-25T00:00:00+05:30");
-
lessThanOrEqualTo
Add a constraint to the query that requires a particular key entry to be less than or equal to the provided value.- Parameters:
key
- The key to be constrainedvalue
- The value that must be equalled.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.lessThanOrEqualTo("due_date", "2013-06-25T00:00:00+05:30");
-
greaterThan
Add a constraint to the query that requires a particular key entry to be greater than the provided value.- Parameters:
key
- The key to be constrained.value
- The value that provides an lower bound.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.greaterThan("due_date", "2013-06-25T00:00:00+05:30");
-
greaterThanOrEqualTo
Add a constraint to the query that requires a particular key entry to be greater than or equal to the provided value.- Parameters:
key
- The key to be constrained.value
- The value that provides an lower bound.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.greaterThanOrEqualTo("due_date", "2013-06-25T00:00:00+05:30");
-
notEqualTo
Add a constraint to the query that requires a particular key's entry to be not equal to the provided value.- Parameters:
key
- The key to be constrained.value
- The object that must not be equaled.- Returns:
Query
object, so you can chain this call.
Example ;
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.notEqualTo("due_date", "2013-06-25T00:00:00+05:30");
-
containedIn
Add a constraint to the query that requires a particular key's entry to be contained in the provided array.- Parameters:
key
- The key to be constrained.values
- The possible values for the key's object.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.containedIn("severity", new Object[]{"Show Stopper", "Critical"});
-
notContainedIn
Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.- Parameters:
key
- The key to be constrained.values
- The list of values the key object should not be.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.notContainedIn("severity", new Object[]{"Show Stopper", "Critical"});
-
exists
Add a constraint that requires, a specified key exists in response.- Parameters:
key
- The key to be constrained.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.exists("status");
-
notExists
Add a constraint that requires, a specified key does not exists in response.- Parameters:
key
- The key to be constrained.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.notExists("status");
-
includeReference
Add a constraint that requires a particular reference key details.- Parameters:
key
- key that to be constrained.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.includeReference("for_bug");
-
tags
Include tags with which to search entries.- Parameters:
tags
- Comma separated array of tags with which to search entries.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.tags(new String[]{"tag1","tag2"});
-
ascending
Sort the results in ascending order with the given key.
Sort the returned entries in ascending order of the provided key.- Parameters:
key
- The key to order by.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.ascending("name");
-
descending
Sort the results in descending order with the given key.
Sort the returned entries in descending order of the provided key.- Parameters:
key
- The key to order by.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.descending("name");
-
except
Specifies list of field uids that would be 'excluded' from the response.- Parameters:
fieldUid
- field uid which get 'excluded' from the response.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
ArrayList<String> array = new ArrayList<String>(); array.add("name"); array.add("description");
csQuery.except(array);
-
except
Specifies list of field uids that would be 'excluded' from the response.- Parameters:
fieldUids
- field uid which get 'excluded' from the response.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.except(new String[]{"name", "description"});
-
only
Specifies an array of 'only' keys in BASE object that would be 'included' in the response.- Parameters:
fieldUid
- Array of the 'only' reference keys to be included in response.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.only(new String[]{"name"});
-
onlyWithReferenceUid
Specifies an array of 'only' keys that would be 'included' in the response.- Parameters:
fieldUid
- Array of the 'only' reference keys to be included in response.referenceFieldUid
- Key who has reference to some other class object.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
ArrayList<String> array = new ArrayList<String>(); array.add("description"); array.add("name");
csQuery.onlyWithReferenceUid(array, "for_bug");
-
exceptWithReferenceUid
Specifies an array of 'except' keys that would be 'excluded' in the response.- Parameters:
fieldUid
- Array of the 'except' reference keys to be excluded in response.referenceFieldUid
- Key who has reference to some other class object.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
ArrayList<String> array = new ArrayList<String>(); array.add("description"); array.add("name");
csQuery.exceptWithReferenceUid(array, "for_bug");
-
count
Retrieve only count of entries in result.- Returns:
Query
object, so you can chain this call. Note :- CallQueryResult.getCount()
method in the success to get count of objects.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.count();
-
includeCount
Retrieve count and data of objects in result- Returns:
Query
object, so you can chain this call. Note :- CallQueryResult.getCount()
method in the success to get count of objects.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.includeCount();
-
includeContentType
Include Content Type of all returned objects along with objects themselves.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.includeContentType();
-
includeOwner
Include object owner's profile in the objects data.- Returns:
- Query object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.includeOwner();
-
skip
The number of objects to skip before returning any.- Parameters:
number
- No of objects to skip from returned objects- Returns:
Query
object, so you can chain this call.Note: The skip parameter can be used for pagination, "skip" specifies the number of objects to skip in the response.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.skip(2);
-
limit
A limit on the number of objects to return.- Parameters:
number
- No of objects to limit.- Returns:
Query
object, so you can chain this call.Note: The limit parameter can be used for pagination, " limit" specifies the number of objects to limit to in the response.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.limit(2);
-
regex
Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large data sets.- Parameters:
key
- The key to be constrained.regex
- The regular expression pattern to match.- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.regex("name", "^browser");
-
regex
Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large data sets.- Parameters:
key
- The key to be constrained.regex
- The regular expression pattern to matchmodifiers
- Any of the following supported Regular expression modifiers.use i for case-insensitive matching.
use m for making dot match newlines.
use x for ignoring whitespace in regex
- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.regex("name", "^browser", "i");
-
language
Deprecated.SetLanguage
instance. -
locale
SetLanguage
instance. -
search
This method provides only the entries matching the specified value.- Parameters:
value
- value used to match or compare- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.search("header");
-
find
Execute a Query and Caches its result (Optional)- Parameters:
callback
-QueryResultsCallBack
object to notify the application when the request has completed.- Returns:
- Query object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.find(new QueryResultsCallBack() {
@Override public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
} });
-
findOne
Execute a Query and Caches its result (Optional)- Parameters:
callBack
-QueryResultsCallBack
object to notify the application when the request has completed.- Returns:
- Query object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.findOne(new QueryResultsCallBack() {
@Override public void onCompletion(ResponseType responseType, ENTRY entry, Error error) {
} });
-
setQueryJson
-
execQuery
protected void execQuery(SingleQueryResultCallback callBack, QueryResultsCallBack callback, boolean isFromLocal) -
getResult
- Specified by:
getResult
in interfaceINotifyClass
-
getResultObject
public void getResultObject(List<Object> objects, org.json.JSONObject jsonObject, boolean isSingleEntry)- Specified by:
getResultObject
in interfaceINotifyClass
-
addParam
This method adds key and value to an Entry. Parameters:- Parameters:
key
- : The key as string which needs to be added to the Queryvalue
- : The value as string which needs to be added to the Query- Returns:
- - Query
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.addParam("key", "some_value"); csQuery.findOne(new QueryResultsCallBack() { @Override public void onCompletion(ResponseType responseType, ENTRY entry, Error error) { } });
-
includeReferenceContentTypUid
This method also includes the content type UIDs of the referenced entries returned in the response- Returns:
Query
Example :
//'blt5d4sample2633b' is a dummy Stack API key //'blt6d0240b5sample254090d' is dummy access token. Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query();
csQuery.includeReferenceContentTypUid(); csQuery.findOne(new QueryResultsCallBack() {
@Override public void onCompletion(ResponseType responseType, ENTRY entry, Error error) {
} });
-
whereIn
Get entries having values based on referenced fields. This query retrieves all entries that satisfy the query conditions made on referenced fields.- Parameters:
key
- The key to be constrainedqueryObject
-Query
object, so you can chain this call- Returns:
Query
object, so you can chain this call
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.whereIn("due_date", csQuery);
-
whereNotIn
Get entries having values based on referenced fields. This query works the opposite of $in_query and retrieves all entries that does not satisfy query conditions made on referenced fields.- Parameters:
key
- The key to be constrainedqueryObject
-Query
object, so you can chain this call- Returns:
Query
object, so you can chain this call
Example :
Stack stack = Contentstack.stack( "APIKey", "deliveryToken", "environment_name"); Query csQuery = stack.contentType("contentType_name").query(); csQuery.whereNotIn("due_date", csQuery);
-
includeFallback
Retrieve the published content of the fallback locale if an entry is not localized in specified locale- Returns:
Query
object, so you can chain this call.
Example :
Stack stack = Contentstack.stack( "ApiKey", "deliveryToken", environment_name); Query csQuery = stack.contentType("contentType_name").query(); csQuery.includeFallback();
-