Support Semantic Search with CoreSpotlight - WWDC24

Peter Yaacoub •


Introduction

At WWDC24, Apple introduced significant enhancements to CoreSpotlight, focusing on semantic search capabilities. This article will guide you through the process of adding your app’s content to Spotlight indexes, enabling a more intelligent and relevant search experience for users.

CoreSpotlight is an Apple framework that allows you to donate content to Spotlight search. The latest update introduces semantic search, which enhances the relevance of search results, particularly for text, image, and video content.

Understanding the Difference

Here’s a comparison between regular search (left) and semantic search (right):

Windsurfing Carmet Windsurfing Carmet
The best windsurfing Sailboarding lessons
Carmel Country The best windsurfing
Windsurfing lessons Carmel Country
  Windsurfing lessons

With semantic search, related terms and synonyms are considered, making search results more intuitive and user-friendly.

Creating Searchable Items

To donate content to Spotlight, you need to create searchable items using CSSearchableItem and CSSearchableItemAttributeSet. Depending on the content type, you’ll need to define specific properties:

let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = "Windsurfing Lessons"
attributeSet.textContent = "Join the best windsurfing lessons at Carmel Beach."

let item = CSSearchableItem(uniqueIdentifier: "com.example.windsurfing", domainIdentifier: "windsurfing", attributeSet: attributeSet)
let searchableIndex = CSSearchableIndex.default()

searchableIndex.indexSearchableItems([item]) { error in
    if let error = error {
        print("Indexing error: \(error.localizedDescription)")
    } else {
        print("Search item successfully indexed!")
    }
}

Indexing Content

Once your searchable items are ready, use the CSSearchableIndex class and CSSearchableIndexDelegate protocol to index them. The beginBatch() and endBatch(withClientState:completionHandler:) methods allow you to batch update the index, ensuring efficient and effective content indexing.

searchableIndex.beginBatch()
searchableIndex.indexSearchableItems([item]) { error in
    if let error = error {
        print("Batch indexing error: \(error.localizedDescription)")
    } else {
        print("Batch indexing successful!")
    }
}
searchableIndex.endBatch(withClientState: nil, completionHandler: nil)

Efficiency Tips

  • Expiration Date: Set the expirationDate property to ensure that outdated content is automatically removed from the index.
  • Update Flag: Use the isUpdate property to indicate when an item is being updated, rather than newly added.

CoreSpotlight Delegate

To handle interactions between the on-device index and your app, create a CoreSpotlight Delegate target. This delegate will manage indexing operations and ensure seamless integration. Testing and debugging can be done using the mdutil Terminal command.

Conclusion

Implementing semantic search with CoreSpotlight can significantly enhance the user experience by providing more relevant and intuitive search results. By following the steps outlined above, you can effectively donate your app’s content to Spotlight, leveraging the power of semantic search.