CoreSpotlight索引

Geo*_*sda 6 indexing ios corespotlight

嗨,我正在尝试在我的应用程序中实现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 *********************");


        }];

    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

sou*_*ned 3

其索引按指定进行。因此,如果您将spotLightIndexing方法放入其中,didFinishLaunchingWithOptions每次启动时都会自然地索引项目,当然除非您设置了布尔值。如果应用程序被删除,它将再次重新索引,因为 NSUserDefault 值将被清零。这就是为什么他们为您提供通过批量更新或此处注释的其他方法添加/更改/更新索引的原因

\n\n

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

\n\n

如果您观看有关此主题的 WWDC 视频,您会发现使用域标识符通过“组”可以轻松更新或删除域。来源这是一块好手表。

\n\n

至于关键字,直到文档完全支持 iOS9 API 后才能得知。但只要阅读苹果公司在这里公开提供的内容,您就应该考虑以下几点:

\n\n
\n

重要提示:请务必避免过度索引您的应用内容或添加不相关的关键字和属性以试图提高结果排名。由于 iOS 衡量用户对搜索结果的参与程度,因此用户认为无用的项目会被快速识别,并最终停止显示在结果中。

\n
\n\n

它位于新的搜索功能摘要之后。它继续说明原因:

\n\n
\n

当您组合多个搜索 API 时,可以从多个位置对项目建立索引。为了避免在搜索结果中向用户提供重复的项目,您需要适当链接项目 ID。为了确保项目 ID 已链接,您可以在可搜索项目\xe2\x80\x99s uniqueIdentifier 属性和 NSUserActivity 对象\xe2\x80\x99s contentAttributes 属性内的 relatedUniqueIdentifier 属性中使用相同的值

\n
\n\n

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

\n\n

就我个人而言,我已经在我的应用程序中实现了这一点并且喜欢它,但是,我使用网络标记,它几乎可以立即进行批量更新,而不是您的路线,在您的路线中,您必须实际推出新的更新才能重新启动-更新/删除索引。

\n