Create a SearchAfterQuery query
You can search for specific entities in Sitecore Content Hub using a SearchAfterQuery query. The SearchAfterQuery does not itself query entities; it describes the querying conditions. It is the querying client that is responsible for running queries and it requires a defined SearchAfterQuery query. The SearchAfterQuery object supports the following parameters:
-
Filter- the type of filter to be invoked. -
Take- the number of entities to be retrieved. -
Sorting- the sorting options to be applied. -
LoadConfiguration- the load configuration to be applied. -
SearchAfter- a list of sort values to retrieve the next page of results. -
Renditions- the renditions to be applied. -
NestedRelations- the nested relations to be applied.
Query filters
You use the QueryFilterBuilder to create filters, as shown in the following examples.
Query by definition name
The following example queries by definition name.
var filter = QueryFilterBuilder.Definition.Name == "M.Asset";Query by definition ID
The following example queries by definition ID.
var filter = QueryFilterBuilder.Definition.Id == 40;Query by created by user
The following example queries by user.
var filter = QueryFilterBuilder.CreatedBy == 6;Query by DateTime property
The following example queries by DateTime property.
var filter =
QueryFilterBuilder.DateTime("created_on") <= DateTime.UtcNow;Query M.Asset entities
The following example queries for M.Asset entities with a title equal to my asset.
QueryNodeFilter filter = QueryFilterBuilder.Definition.Name == "M.Asset" &&
QueryFilterBuilder.String("Title") == "my asset";
var searchAfterQuery = new SearchAfterQuery(
filter,
new List<Sorting>{new() { Field = "identifier", Order = QuerySortOrder.Asc }})
{
Take = Take,
LoadConfiguration = new EntityLoadConfiguration
{
CultureLoadOption = new CultureLoadOption(new CultureInfo("en-us")),
PropertyLoadOption = PropertyLoadOption.All,
RelationLoadOption = RelationLoadOption.All
}
};
IEntitySearchAfterResult result = await _client.Querying.SearchAfterAsync(searchAfterQuery).ConfigureAwait(false);
// query for the next set of results
searchAfterQuery.SearchAfter = result.LastHitData;
result = await _client.Querying.SearchAfterAsync(searchAfterQuery).ConfigureAwait(false);Query by ancestor
The following example retrieves a page of entities that are linked to a specific ancestor through a relation, sorted by creation date and then by identifier to handle cases where one or more entities have the same creation date. It is culture-aware and optimized for pagination using SearchAfterQuery.
var ancestorQueryFilter = QueryFilterBuilder.Ancestor(relation: Constants.Asset.AssetTypeToAsset, id: ancestorId);
var searchAfterQuery = new SearchAfterQuery(ancestorQueryFilter, new List<Sorting> { new() { Field = "created_on",Order = QuerySortOrder.Desc }, new() { Field = "identifier",Order = QuerySortOrder.Desc } })
{
Take = Take,
LoadConfiguration = new EntityLoadConfiguration
{
CultureLoadOption = new CultureLoadOption(new CultureInfo("en-us")),
PropertyLoadOption = PropertyLoadOption.All,
RelationLoadOption = RelationLoadOption.All
}
};
IEntitySearchAfterResult result = await _client.Querying.SearchAfterAsync(searchAfterQuery);