CoreSpotlight Indexing

Hi, I am trying to implement CoreSpotlight in my application.

When indexing, do I need to run it every time, or is it enough to run this time when the application is installed for the first time? If the application is deleted, do I need to index again?

Here is 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(@"******************* ERROR *********************"); }]; } } 

Thank you

+5
source share
1 answer

It is indexed as indicated. Therefore, if you put your spotLightIndexing method in didFinishLaunchingWithOptions , it will naturally index the elements of each run, unless you set bool, of course. If the application is uninstalled, it will be indexed again, since the NSUserDefault values ​​will be reset. That's why they suggest you add / modify / update indexes using batch updates or other methods as annotated here

Since you are populating it from a local plist unlike the Internet, you will need to do the updates yourself or create an application extension to support the index.

If you watch the WWDC video in this section, you will see that it is easy to update or delete domains as a β€œgroup” using a domain identifier. Source Good watch.

As for keywords, until it is confirmed that the documents fully support the iOS9 API. But just by reading what Apple has publicly provided here, you should take note:

Important. Be sure to avoid over-indexing the contents of your application or adding unrelated keywords and attributes in an attempt to improve the ranking of your results. Since iOS measures the level of interaction with users with search results, elements that users do not find useful are quickly identified and may eventually stop appearing in the results.

This is located after a new summary of search functions. And he goes on to say why:

When combining multiple search APIs, items can be indexed from multiple locations. To avoid providing users with duplicate elements in search results, you need to bind element identifiers accordingly. To make sure that the item identifiers are related, you can use the same value in the uniqueIdentifier property of search objects and in the relatedUniqueIdentifier property in the contentAttributes property of the NSUserActivity object

In other words, say that you have enabled NSUserActivity , because they are for you, because it can be applied to all users of your application, and not just to the person performing the request, it can be repeatedly populated in the same search. Therefore, based on suggestions from Apple, try not to use keywords unless you are sure, especially based on your example, where the keyword is already = uniqueIdentifier.

Personally, I have already implemented this in my application and love it, however, I use web markup, which makes batch updates almost instantly, unlike your route, where you have to actually push a new update to re-update / delete indexes.

+2
source

All Articles