Search contacts and Commerce interactions in the xDB index

Abstract

How to query commerce models and properties in the xDB index on Azure.

Using the xConnect Search API, you can search contacts and Commerce interactions to produce, for example, a list of customers who have placed orders or a list of all product purchased by a customer.

Note

You can find a blacklist of all models that are not indexed by default. These models have the DoNotIndexAttribute type. This list is contained in the Sitecore.Commerce.Connect.XConnect.Models.json file that is deployed with a custom model.

This topic provides examples of how you can use the xConnect Search API to query contacts based on Commerce events.

Note

On Azure, you can put the nested loops in the following examples into a single LINQ statement.

The following code example returns a list of contacts who have placed orders.

using (var client = new XConnectClient(cfg))
            {
                var query = client.Contacts.Where(c => c.Interactions
                    .Any(i => i.Events.OfType<VisitorOrderCreatedGoal>()
                        .Any(e => e.Order.CartLines.Any(l => l.Product.ProductName == "Habitat Sentinel Touchscreen Thermostat"))));

                var enumerator = await query.GetBatchEnumerator(20);
                List<IEntityReference<Contact>> contactsWhoPlacedOrders = new List<IEntityReference<Contact>>();

                while (await enumerator.MoveNext())
                {
                    var contacts = enumerator.Current;
                    contactsWhoPlacedOrders.AddRange(contacts);
                }
            }

The following code example returns a list of products that have been purchased by a specific customer.

using (var client = new XConnectClient(cfg))
            {
                var contactId = Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e");
                var query = client.Interactions
                    .Where(i => i.Contact.Id == contactId
                        && i.Events.OfType<VisitorOrderCreatedGoal>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var productList = interactions.Where(i => i.Events.OfType<VisitorOrderCreatedGoal>().Any())
                        .SelectMany(i => i.Events.OfType<VisitorOrderCreatedGoal>())
                            .SelectMany(e => e.Order.CartLines)
                            .Select(cl=>cl.Product).ToList();
                }
            }

The following code example returns a list of all the products that have been viewed by an individual customer.

using (var client = new XConnectClient(cfg))
            {
                var query = client.Interactions
                    .Where(i => i.Contact.Id == Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e")
                        && i.Events.OfType<VisitedProductDetailsPageEvent>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var visitedProductsListIds = interactions
                        .SelectMany(i => i.Events.OfType<VisitedProductDetailsPageEvent>())
                        .Select(e => e.ProductId).ToList();
                }
            }

The following code example returns a list of all the categories that a customer visited.

var cfg = GetXConnectClientConfiguration();

            using (var client = new XConnectClient(cfg))
            {
                var query = client.Interactions
                    .Where(i => i.Contact.Id == Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e")
                        && i.Events.OfType<VisitedCategoryPageEvent>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var visitedCategoryListIds = interactions
                        .SelectMany(i => i.Events.OfType<VisitedCategoryPageEvent>())
                        .Select(e => e.CategoryId).ToList();
                }
            }

The following code example returns a list of customers who have abandoned their carts.

var cfg = GetXConnectClientConfiguration();

            using (var client = new XConnectClient(cfg))
            {
                var query = client.Contacts.Where(c => c.Interactions
                    .Any(i => i.Events.OfType<AbandonedCartPageEvent>().Any()));
                var enumerator = await query.GetBatchEnumerator(20);
                List<IEntityReference<Contact>> contactsWithAbandonedCart = new List<IEntityReference<Contact>>();

                while (await enumerator.MoveNext())
                {
                    var contacts = enumerator.Current;
                    contactsWithAbandonedCart.AddRange(contacts);
                }
            }