【问题标题】:Indexing content using Core Spotlight使用 Core Spotlight 索引内容
【发布时间】:2016-07-28 04:20:27
【问题描述】:

在邮件应用程序或消息应用程序中,您可以使用 Core Spotlight 搜索来搜索任何消息的内容。我也可以看到 OneNote 这样做,所以它应该在 API 中可用。

但是,几乎没有关于此的文档。我只能看到CSSearchableItemAttributeSet 中有contentUrl,但我尝试设置 .txt 文件的 NSUrl 并没有发生任何事情。还尝试将 contentType 设置为 kUTTypeTextkUTTypeUTF8PlainText,但没有任何改进。

是否需要某种特定的文件格式?还是其他人应该做的?

【问题讨论】:

  • 所以你的数据是消息?您当前正在为哪些属性设置值?主题或文本内容?
  • 为什么你不能在你的应用程序中深度链接 .txt 文件。我知道这对您的情况来说是一个技巧,但是也可以解决您的问题。要进行深度链接,请设置唯一标识符并在 AppDelegate.m 中的 -(BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler{} 中获取它
  • @Wain,数据由用户存储,因此理论上它可以是任何文本,通常长度约为 5000 个字符,尽管它可以是任何长度。我设置了 title 和 thumbnailUrl,如果项目来自网络,我还设置了 contentSources。
  • @SanchitKumarSingh 我不介意黑客攻击,我已经有一些深度链接,并且我正在使用您在这些情况下提到的 API。但是,我看不出它有什么帮助。你指的是nativescript.org/blog/details/…之类的东西吗?我相信你应该在下面回答,即使它不是一个完整的解决方案,如果有一些有用的信息并且至少是部分解决方案有人(包括我)可能会投票支持它。

标签: ios corespotlight


【解决方案1】:

Apple documentation on CoreSpotlight 分解了创建项目并将其添加到可搜索索引的过程:

  • 创建一个 CSSearchableItemAttributeSet 对象并指定描述您要索引的项目的属性。

  • 创建一个 CSSearchableItem 对象来表示该项。 CSSearchableItem 对象具有唯一标识符,可让您引用 以后再说吧。

  • 如果需要,请指定域标识符,以便您可以将多个项目聚集在一起并将它们作为一个组进行管理。

  • 将属性集与可搜索项相关联。

  • 将可搜索项添加到索引中。

下面是一个简单的示例 I,它展示了如何索引一个简单的 Note 类:

class Note {
    var title: String
    var description: String
    var image: UIImage?

    init(title: String, description: String) {
        self.title = title
        self.description = description
    }
}

然后在其他一些函数中,创建你的笔记,为每个笔记创建一个CSSearchableItemAttributeSet,从属性集中创建一个唯一的CSSearchableItem,并索引可搜索项的集合:

import CoreSpotlight
import MobileCoreServices

// ...

// Build your Notes data source to index
var notes = [Note]()
notes.append(Note(title: "Grocery List", description: "Buy milk, eggs"))
notes.append(Note(title: "Reminder", description: "Soccer practice at 3"))
let parkingReminder = Note(title: "Reminder", description: "Soccer practice at 3")
parkingReminder.image = UIImage(named: "parkingReminder")
notes.append(parkingReminder)

// The array of items that will be indexed by CoreSpotlight
var searchableItems = [CSSearchableItem]()

for note in notes {
    // create an attribute set of type Text, since our reminders are text
    let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)

    // If we have an image, add it to the attribute set
    if let image = note.image {
        searchableItemAttributeSet.thumbnailData = UIImagePNGRepresentation(image)
        // you can also use thumbnailURL if your image is coming from a server or the bundle
//        searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource("image", withExtension: "jpg")
    }

    // set the properties on the item to index
    searchableItemAttributeSet.title = note.title
    searchableItemAttributeSet.contentDescription = note.description

    // Build your keywords
    // In this case, I'm tokenizing the title of the note by a space and using the values returned as the keywords
    searchableItemAttributeSet.keywords = note.title.componentsSeparatedByString(" ")

    // create the searchable item
    let searchableItem = CSSearchableItem(uniqueIdentifier: "com.mygreatapp.notes" + ".\(note.title)", domainIdentifier: "notes", attributeSet: searchableItemAttributeSet)
}

// Add our array of searchable items to the Spotlight index
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) in
    if let error = error {
        // handle failure
        print(error)
    }
}

这个例子改编自AppCoda's How To Use Core Spotlight Framework in iOS 9指南。

【讨论】:

  • 我有一些cmets,但删除了它们并做了实验。在不那么短的电子邮件(18600 个字符)中,每个单词都是可搜索的,包括像“this”这样的常见单词。我相信它排除了使用描述,很可能也排除了关键字。你同意吗?
  • @IvanIčin 可能 OneNote 有一个代词列表或其他列入黑名单的词,这些词在创建可搜索项索引之前已从可​​搜索文本中删除。
猜你喜欢
  • 2016-03-13
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 2016-05-08
  • 2015-08-25
  • 2020-02-10
  • 1970-01-01
相关资源
最近更新 更多