Search contacts
This topic shows you how to how to query contacts. Each example uses the client.GetBatchEnumerator() / client.GetBatchEnumeratorSync() to paginate results. Examples that rely on joins have an equivalent example that uses the InteractionsCache facet for search providers that do not support joins.
Refer to the list of supported methods for xConnect search.
Search contacts by ID
Search by ID is not supported. If you know the contact’s ID, you can get the contact from the collection database.
Search contacts by identifier source and type
Identifiers are stored in an encrypted field in the xDB Collection database and are not indexed. You can search by identifier source or type, or use an identifier and identifier source to get a contact from the xDB Collection database.
Search contacts by identifier source
The following example shows you how to get all contacts with at least one identifier where the value of the Source property is twitter. Results are returned in batches of 10. For each contact, a new identifier with the source tweeter is added and the old identifier is removed (identifiers cannot be edited). Operations are submitted to xConnect in batches of approximately 200.
Search contacts by identifier type
In the following example, all contacts with at least one known identifier are returned.
Search by by facet property value
In the following example, all contacts with the job title Programmer Writer are returned. To include facets in the results, you must use the .WithExpandOptions method as shown.
If you are not indexing PII sensitive data, you cannot search by facets or facet properties that are marked PII sensitive. For example, a contact’s first name and last name are marked as PII sensitive.
Search by existence of contact facet
It is not possible to search for null values. In order to determine whether or not a facet has been set, you should search for the property that is most likely to have a value. For example:
-
You must pass a preferred email address into the constructor of the
EmailAddressListclass. Therefore, you can rely on thePreferredEmail.SmtpAddressproperty being populated. -
The
PersonalInformationclass does not have any mandatory properties. Therefore, you must check the property that is most likely to have been set.
In the following example, any contact with the EmailAddressList and PersonalInformation facets is returned. This example relies on being able to search for the contact’s first name, which is PII sensitive data:
Search by nested facet property value
The following example returns all contacts that have addresses in Bristol. Expand options are used to return the AddressList facet with the results.
You must check the AddressList.Preferred property and the AddressList.Others property.
Using facet extension methods
You can use facet extension methods in the context of a query. The following example demonstrates how to retrieve the AddressList facet using the .GetFacet<AddressList> method and the .Addresses() method:
Search by contact behavior
Joins are only possible if the search provider supports joins. Use the example that suits your requirements.
Contact and interaction search can be combined to return contacts whose interactions match particular criteria. To search interactions, use interaction search.
The query returns all contacts with interactions that match the query. However, interactions that are returned using expand options do not necessarily match the query parameters.
Search by interaction date
The following examples demonstrate how to retrieve contacts that have an interaction that is older than five days. In addition, expand options are used to return the contact’s top 20 interactions. These interactions do not necessarily match the query parameters.
With joins
The following example uses joins to get any contact with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet to get contacts with one or more matching interactions.
Search by interaction facets
The following examples demonstrate how to retrieve contacts that have an interaction with a specific referrer.
With joins
The following example uses joins to get contacts with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet to get contacts with one or more matching interactions.
Search by interaction events
The following example demonstrates how to search for contacts that have triggered an event that matches the following criteria:
-
Where the event type is
Goal -
Where the event definition ID is
{29408b2d-52b6-4f39-96ca-039cd96f4624}
With joins
The following example uses joins to get contacts with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet and is supported by all search providers, including providers that do not support joins:
Search by recent activity
EngagementMeasures and KeyBehaviorCache are calculated facets that store information about a contact’s recent behavior. Querying these facets does not require joins as they both belong to the contact. The following examples demonstrate how to use the EngagementMeasures and KeyBehaviorCache to return contacts whose recent behavior match a specific set of criteria.
Engagement measures
The following query returns all contacts with a recent interaction with a duration of over thirty seconds:
Key behavior cache
The following query returns all contacts with a recent interaction that took place at a venue with the definition ID 951fb783-5959-49a4-a1f3-ced3453725a4.
Search by expanded DateTime property
In Sitecore 10.0 and later, you can add the [ExpandDate] attribute to a DateTime facet property to enable searching by day, month, and/or year, rather than the entire DateTime property value.
The following example shows you how to search for all contacts born in September using the PersonalInformation.Birthdate property:
The PersonalInformation.Birthdate property is nullable. For non-nullable dates use, .Month instead of .Value.Month. For example:
Partial text search
Partial text search is only supported by the Solr provider for xConnect search.
In Sitecore 10.0 and later, you can perform a partial text search against a configurable list of contact facets in a single query. Some PersonalInformation facet data is added to the textmatch field in Solr by default, such as first name, last name, and email address. You can extend the configuration to include additional facets.
The following example returns any contact where the first name, last name, or email address starts with the string Mar:
You cannot perform a partial text search against a specific facet.
Results and pagination
The .GetBatchEnumerator() or .GetBatchEnumeratorSync() extension methods are the recommended way to return results from a query. See the overview of pagination for more information.
For each method, the maximum size of a single batch is hardcoded to Sitecore.XConnect.SearchExtensions.DefaultBatchSize, which is set to 1000. This value is currently not configurable.
|
Asynchronous | |
|
Method |
Notes |
|
|
Recommended way of paginating results. |
|
|
Can be used with |
|
|
Can be used with |
|
Synchronous | |
|
Method |
Notes |
|
|
Recommended way of paginating results. |
|
|
Can be used with |
The following example demonstrates how to use each method to return results:
Ordering results
You can orders results by a contact’s facets or properties. The following example shos you how to order contacts by the MostRecentInteractionStartDateTime property on the EngagementMeasures facet:
You cannot order by the properties of a list. For example, you cannot order a list of contacts by the StartDateTime of its interactions.
Expand options
Use the .WithExpandOptions() method to specify which facets and related interactions to return with each contact. These expand options are identical to the ones that you use when retrieving a contact by ID or identifier. If you do not specify any expand options, no contact facets or related interactions are returned.
In the following example we are:
-
Retrieving one contact facet.
-
Retrieving one interaction facet.
-
Retrieving a maximum of 3 interactions per contact.
Specifying a related interaction limit of 1 ensures that only the latest interaction is returned.
Related interactions are not affected by the query
The following query returns all contacts with at least one interaction that has the WebVisit facet. The query also returns the contact’s top 30 interactions:
The Interactions collection is not affected by the query. The query returns all contacts with at least one matching contact from any point in time. However, the interaction that results in a match is not necessarily present in every contact’s Interactions property.
Count contacts
Use the following method to return a count of all matching contacts without returning any contact data: