CoreSpotlight 索引

CoreSpotlight indexing

您好,我正在尝试在我的应用程序中实施 CoreSpotlight。

编制索引时,我需要每次 运行 还是第一次安装应用程序 运行 一次就足够了? 如果应用被删除,我需要重新索引吗?

这是我使用的代码:

- (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 *********************");


        }];

    }
}

谢谢

它已按指定编入索引。所以如果你把你的 spotLightIndexing 方法放在 didFinishLaunchingWithOptions 中,它会在每次启动时自然地索引项目,当然除非你设置了一个布尔值。如果应用程序被删除,它将再次重新索引,因为 NSUserDefault 值将被清零。这就是为什么他们通过批量更新或其他方法为您提供 add/altering/updating 索引 here

由于您是从本地 plist 而不是 Web 填充它,因此您必须自己进行更新或创建一个索引维护应用程序扩展。

如果您观看有关此主题的 WWDC 视频,您会发现 'group' 使用域标识符可以轻松更新或删除域。 Source 手表不错。

至于关键字,在文档完全支持 iOS9 API 之前是无从得知的。但只要阅读 Apple 在这里公开提供的内容,您就应该考虑一下:

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.

它位于新的搜索功能摘要之后。它接着说为什么:

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

换句话说,假设您按照他们的意愿合并 NSUserActivity,因为它可以适用于您应用的 所有 用户,而不仅仅是执行此操作的人查询时,它可以在同一搜索中多次填充。因此,根据 Apple 的建议,除非您确定,否则请尽量不要使用关键字,尤其是根据您的示例,其中关键字已经 = uniqueIdentifier。

就我个人而言,我已经在我的应用程序中实现了它并且很喜欢它,但是,我使用网络标记几乎可以即时进行批量更新,而不是你的路线,你必须实际推出一个re-update/delete 索引的新更新。