How do I add my app to the semantic index
Asked on 2024-07-30
1 search
To add your app to the semantic index, you can follow these steps:
-
Donate Searchable Content: Start by donating searchable content to Spotlight. This involves creating
CSSearchableIteminstances for the content you want to index. Each item should have a unique identifier and an attribute set that describes the content.let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) attributeSet.title = "Journal Entry Title" attributeSet.contentDescription = "Description of the journal entry" let item = CSSearchableItem(uniqueIdentifier: "unique-id", domainIdentifier: "com.example.journal", attributeSet: attributeSet) -
Index the Items: Use the
CSSearchableIndexto index these items. You can batch index items to improve performance.let searchableIndex = CSSearchableIndex.default() searchableIndex.indexSearchableItems([item]) { error in if let error = error { print("Indexing error: \(error.localizedDescription)") } else { print("Search item successfully indexed!") } } -
Use the IndexedEntity Protocol: If your app uses app entities, you can conform to the
IndexedEntityprotocol to make these entities searchable. This allows you to leverage the new semantic search capabilities.struct Trail: IndexedEntity { var id: String var name: String var description: String var searchableItem: CSSearchableItem { let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) attributeSet.title = name attributeSet.contentDescription = description return CSSearchableItem(uniqueIdentifier: id, domainIdentifier: "com.example.trails", attributeSet: attributeSet) } } -
Handle Reindexing Requests: Implement an index delegate extension to handle reindexing requests from Spotlight. This ensures that your app can update the index as needed.
class IndexDelegate: NSObject, CSSearchableIndexDelegate { func searchableIndex(_ searchableIndex: CSSearchableIndex, reindexAllSearchableItemsWithAcknowledgementHandler acknowledgementHandler: @escaping () -> Void) { // Reindex all items acknowledgementHandler() } func searchableIndex(_ searchableIndex: CSSearchableIndex, reindexSearchableItemsWithIdentifiers identifiers: [String], acknowledgementHandler: @escaping () -> Void) { // Reindex specific items acknowledgementHandler() } }
For a detailed walkthrough, you can refer to the session Support semantic search with Core Spotlight.
Relevant Sessions

Support semantic search with Core Spotlight
Learn how to provide semantic search results in your app using Core Spotlight. Understand how to make your app’s content available in the user’s private, on-device index so people can search for items using natural language. We’ll also share how to optimize your app’s performance by scheduling indexing activities. To get the most out of this session, we recommend first checking out Core Spotlight documentation on the Apple Developer website.

Bring your app to Siri
Learn how to use App Intents to expose your app’s functionality to Siri. Understand which intents are already available for your use, and how to create custom intents to integrate actions from your app into the system. We’ll also cover what metadata to provide, making your entities searchable via Spotlight, annotating onscreen references, and much more.

What’s new in App Intents
Learn about improvements and all-new features with App Intents, and discover how this framework can help you expose your app’s functionality to Siri, Spotlight, Shortcuts, and more. We’ll show you how to make your entities more meaningful to the platform with the Transferable API, File Representations, new IntentFile APIs, and Spotlight Indexing, opening up powerful functionality in Siri and the Shortcuts app. Empower your intents to take people deep into your app with URL Representable Entities. Explore new techniques to model your entities and intents with new APIs for error handling and union values.
