Create a query
You can use the Querying client to create queries and then run any of the following queries or iterate on results.
View query results
After running a query, an IQueryResult is returned. This result has the following properties:
-
Items- the entities/IDs that matched the query, which can be a subset of the total results. -
TotalNumberOfResults- the number of items that matched the query in Sitecore Content Hub (even if not all of them are inItems). -
Offset- the number of items that were skipped.
Query for a single entity
When it is certain that a query only matches a single entity, you can use the singleAsync method.
When multiple items match the query, an InvalidOperationException is thrown.
Query for a single entity ID
When it is certain that a query only matches a single entity, it is convenient to use the singleIdAsync method.
When multiple items match the query, an InvalidOperationException is thrown.
Query for one or multiple entities
To query for one or more entities:
The IEntityQueryResult.Items contains the returned entities.
Query for one or multiple entity ids
To query for one or more entities:
The IIdQueryResult.Items contains the returned entities.
Iterate query result entities
When the query returns a small to medium size of results (the default configuration is 10,000 results maximum), it is easy to batch process the entities using an iterator.
In the following code snippet, an iterator is created:
In the following code snippet, the iterator is used to batch process entities:
The while loop automatically ends when the iterator has finished processing all the batches of entities.
When the iterator goes beyond the configured maximum number of results, the server throws an exception. In this case, a scroll is necessary.
Iterate query result entity IDs
When the query returns a small to medium size of results (the default configuration is 10.000 results maximum), it is easy to batch process the entity IDs using an iterator.
In the following code snippet, an iterator is created:
In the following code snippet, the iterator is used to batch process entity IDs:
The while loop automatically ends when the iterator has finished processing all the batches of entities.
When the iterator goes beyond the configured maximum number of results, the server throws an exception. To solve this issue, you can use another query with a scroll. The scroll will be used to skip the first few elements.
The following example shows how to skip the first 200 elements using the scroll: