【问题标题】:CoreSpotlight indexingCoreSpotlight 索引
【发布时间】:2015-11-02 07:04:18
【问题描述】:

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


        }];

    }
}

谢谢

【问题讨论】:

  • 你的代码有效吗?因为当我把这段代码放在我的项目编译器上时,我没有Search item indexed 输出!
  • @Mc.Lover 是的,它就像一个魅力!请参阅已编辑的答案以记录任何错误
  • 请您看看这个问题好吗? stackoverflow.com/questions/33443833/…,我使用了你的代码,但是没有用!

标签: ios indexing corespotlight


【解决方案1】:

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

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

如果您观看有关此主题的 WWDC 视频,您会发现使用域标识符按“组”更新或删除域很容易。 Source 这是一块好表。

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

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

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

当您组合多个搜索 API 时,项目可以从多个位置获得索引。为避免在搜索结果中为用户提供重复的项目,您需要适当地链接项目 ID。为确保项目 ID 被链接,您可以在可搜索项目的 uniqueIdentifier 属性和 NSUserActivity 对象的 contentAttributes 属性中的 relatedUniqueIdentifier 属性中使用相同的值

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

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

【讨论】:

  • 感谢您的洞察力。使用本地 plist 是我们做出的决定,而不是纯粹的网络,因为如果 plist 发生变化,那么无论如何都必须更新应用程序才能合并新的条目/删除。
  • 您是否能够测试您的网络标记是否被索引?这在开发/调试中是如何工作的?
  • 您能否详细说明批量索引 - (void)beginIndexBatch 和 endIndexBatchWithClientState:completionHandler ?
  • 嘿@guhan0 我不知道你到底需要什么,但如果这是一个单独的问题可以让每个人受益,我建议创建一个不同的问题,以便未来的问题寻求者可以很容易找到它,因为这个问题特定于索引频率,而不是如何索引,即使标题具有误导性
  • 很遗憾我的问题被屏蔽了:(
猜你喜欢
  • 1970-01-01
  • 2016-11-28
  • 2016-12-06
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2015-10-27
  • 2015-09-12
  • 1970-01-01
相关资源
最近更新 更多