CoreSpotlight indexing

核能气质少年 提交于 2019-12-21 12:52:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/31355749/corespotlight-indexing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!