AppIntents + CSSearchableItemAttributeSet: only displayName indexed?

On iOS 18, I'm trying to index documents in Spotlight using the new combination of AppIntents+IndexedEntity.

However, I don't seem to be able to index the textContent of the document. Only the displayName seems to be indexed.

As recommended, I start with the defaultAttributeSet:

/// I call this function to index in Spotlight
    static func indexInSpotlight(document: Document) async {
        do {
            if let entity = document.toEntity {
                try await CSSearchableIndex.default().indexAppEntities([entity])
            }
        } catch {
            DLog("Spotlight: could not index document: \(document.name ?? "")")
        }
    }

/// This is the corresponding IndexedEntity with the attributeSet
@available(iOS 18, *)
extension DocumentEntity {
    var attributeSet: CSSearchableItemAttributeSet {
        let attributeSet = defaultAttributeSet
        attributeSet.title = title
        attributeSet.displayName = title
        attributeSet.textContent = docContent
        attributeSet.thumbnailData = thumbnailData
        attributeSet.kind = "document"
        attributeSet.creator = Constants.APP_NAME
        return attributeSet
    }
}

How can I have more that the displayName to be indexed? Thanks :-)

One important property that I see you are missing is the contentType property on the attribute set. That should come from a UTType, either one of the system ones from the UniversalTypeIdentifiers framework, or one that you construct as a UTType to match your custom exported document type declared in your Info.plist. What happens if you add that?

— Ed Ford,  DTS Engineer

I looked into this further today, and the correct way to do this could be an "it depends" type of situation. While I'd like to hear what happens if you try my suggestion about the content type above, it's also worth discussing further once you run that test. Would it be possible for you to also create a small test app that you can share with me? If you're not familiar with preparing a test project, take a look at Creating a test project.

— Ed Ford,  DTS Engineer

So I ran further tests:

  1. Setting contentType: no success -> title is indexed, but not textContent
attributeSet.contentType = UTType.plainText.identifier
attributeSet.textContent = ocrText
  1. I tried printing the entity textContent just before indexing:
NSLog("Indexing entity with OCR = \(entity.attributeSet.textContent ?? "")")
try await CSSearchableIndex.default().indexAppEntities([entity])

It correctly prints out the textContent of the entity. --> Core Spotlight indexes the entity, but seems to ignore the textContent field

  1. Modified the AppIntentsJournal project from Apple, to fill in the textContent field in the indexed attribute set.

--> textContent is correctly indexed in that project The entity are almost identically the same in both projects. What could cause a difference in behavior from CoreSpotlight?

What if you take two paths? You can index your app entities, but what if you separately add your data to the Spotlight index with indexSearchableItems(_:completionHandler:)? And for testing purposes to confirm that much works, I'd temporarily remove indexing the app entities, ensure that implementation works the way you're expecting, and then add back the app entities in the index later.

You've likely hit upon something that's going to be of interest to my colleagues who work on these APIs, so in addition to the suggestion above, I'd like you to file a bug report with a small test project attached so we can see the exact code you have and how we can improve the API behaviors to meet your expectations. Please post that FB number here so I that I can review it.

— Ed Ford,  DTS Engineer

@DTS Engineer : I filed the bug report:** FB16995719**

I tested the old way for indexing: indexSearchableItems(_:completionHandler:)

The issue remains the same. The textContent is NOT found when using Spotlight.

However, querying Spotlight on textContent within the app, returns the right result.

This is so strange.

Best regards

Not fixed under iOS 18.4, unfortunately :(

AppIntents + CSSearchableItemAttributeSet: only displayName indexed?
 
 
Q