CoreSpotlight indexing
Asked Answered
E

1

6

Hi I'm trying to implement CoreSpotlight in my app.

When indexing do I need to run this every time or is it sufficient to run this once when app is installed for the first time? If app is deleted do I need to index again?

Here's the code I'm using:

- (void)spotLightIndexing {

    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"aDetailed" ofType:@"plist"];

    NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray *plistArray = [plistDict allKeys];

    for (id key in plistDict) {

        CSSearchableItemAttributeSet* attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(NSString *)kUTTypeImage];

        // Set properties that describe attributes of the item such as title, description, and image.

        attributeSet.title = key;
        attributeSet.contentDescription = [plistDict objectForKey:key];

//*************************************

 attributeSet.keywords = plistArray; // Another Q: do i need this????

//**************************************  

        // Create an attribute set for an item

        UIImage *image = [UIImage imageNamed:@"icon.png"];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
        attributeSet.thumbnailData = imageData;

        // Create a searchable item, specifying its ID, associated domain, and the attribute set you created earlier.

        CSSearchableItem *item;
        NSString *identifier = [NSString stringWithFormat:@"%@",attributeSet.title];

        item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:@"com.example.apple_sample.theapp.search" attributeSet:attributeSet];

        // Index the item.

        [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler: ^(NSError * __nullable error) {
                        if (!error)
           NSLog(@"Search item indexed");
                       else {
                            NSLog(@"******************* E R R O R *********************");


        }];

    }
}

thank you

Estop answered 11/7, 2015 at 9:42 Comment(3)
Does you code works ?? because when I put this code on my project compiler gives me no Search item indexed output !Treacherous
@Mc.Lover yes it works like a charm! see the EDITED answer for logging any errorsEstop
Would you please take a look at this question ? #33444333 , I used your code but didn't work !Treacherous
S
3

Its indexed as specified. So if you put your spotLightIndexing method in didFinishLaunchingWithOptions it will of naturally index items every launch, unless you set a bool of course. If the app is deleted it will re-index again as the NSUserDefault values will be zeroed out. That is why they offer you add/altering/updating indices via batch updates or other methods as annotated here

Since your populating it from a local plist as opposed to the web, you will have to do the updates yourself or create an index-maintenance app extension.

If you watch the WWDC video on this topic, you will see that's easy to update or delete domains by a 'group' using the domain identifier. Source It's a good watch.

As far as the keywords, there is no telling until the documents are fully supporting iOS9 APIs. But just by reading what Apple has publicly provided here is a note you should consider :

Important: Be sure to avoid over-indexing your app content or adding unrelated keywords and attributes in an attempt to improve the ranking of your results. Because iOS measures the level of user engagement with search results, items that users don’t find useful are quickly identified and can eventually stop showing up in results.

That is located after the new Search features summary. And it goes on to say why:

When you combine multiple Search APIs, items can get indexed from multiple places. To avoid giving users duplicate items in search results, you need to link item IDs appropriately. To ensure that item IDs are linked, you can use the same value in a searchable item’s uniqueIdentifier property and in the relatedUniqueIdentifier property within an NSUserActivity object’s contentAttributes property

So in other words, say you incorporate NSUserActivity as they intend you to because it can apply to all users of your app, not just the person doing the querying, it can populate multiple times in the same search. So, based on Apples suggestions, try not to use keywords unless your sure, especially based off your example, where the keyword already = uniqueIdentifier.

Personally, i've already implemented this into my app and love it, however, I use web mark-up which makes batch updates almost instantaneously, as opposed to your route, where you would have to actually push out a new update to re-update/delete the indices.

Sprig answered 14/7, 2015 at 22:57 Comment(6)
thanks for the insight. Using a local plist is a decision we made as opposed to the web purely because if the plist changes then the app has to be updated anyway in order to incorporate the new entries/deletions.Estop
Have you been able to test your web-markup is being indexed? How does that work in development/debugging?Lucillalucille
could you elaborate about batch indexing - (void)beginIndexBatch and endIndexBatchWithClientState:completionHandler ?Darryldarryn
hey @guhan0 I don't know what you need exactly, but if it's a separate question that can benefit everyone, I suggest creating a different question so future question seekers can find it easily as this question is particular to frequency of indexing, not how-to index, even though the title is misleadingSprig
unfortunately my questions are blocked :(Darryldarryn
@Sprig Is it possible to index NSuserActivity using for loop for different data? I just tried but only last item indexed item is shown in search results.Pyromancy

© 2022 - 2024 — McMap. All rights reserved.