简体   繁体   English

如何在CloudKit记录中搜索部分单词?

[英]How do I search for a partial word in a CloudKit record?

I'm trying to search for CloudKit records containing search text entered by my users. 我正在尝试搜索包含用户输入的搜索文本的CloudKit记录。

I have records with these titles (in String format): 我有这些标题的记录(字符串格式):

  • Item 1 项目1
  • Item 2 项目2
  • Item 3 项目3
  • Test 1 测试1
  • Test 2 测试2

I have tried using 'contains': 我尝试使用“包含”:

let predicate = NSPredicate(format: "self contains %@", searchText) 
let query = CKQuery(recordType: "ShareableItem", predicate: predicate)
    publicDatabase.perform(query, inZoneWith: nil) { results, error in
    ...

But this only returns the results I'm looking for if I use a complete word as a search term. 但是,如果我使用完整的单词作为搜索词,这只会返回我要查找的结果。

EG. 例如。 If I search for 'Item', I get back Item 1,Item 2 and Item 3 but if I search for 'tem' I don't get any results. 如果我搜索“项目”,则会返回项目1,项目2和项目3,但是如果我搜索“ tem”,则不会得到任何结果。

Is there a way of including a wildcard character in the search or another way of using partial search text? 是否可以在搜索中包含通配符或使用部分搜索文本的另一种方法?

I've also tried: 我也尝试过:

NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", searchText)

but with the same result 但结果相同

You can only search for partial texts in from the beginning of a field. 您只能从字段的开头搜索部分文本。 For instance you could use: 例如,您可以使用:

NSPredicate(format: "YourTextField BEGINSWITH %@", searchText)

The token matches query searches for tokens (words) in your record. 令牌与查询匹配,以搜索记录中的令牌(单词)。 It will always search for complete words. 它将始终搜索完整的单词。 So far I have only found one workaround for this issue and that is that you take the field where you want to search in en generate an extra field with all the partial tokens that you can generate. 到目前为止,我只找到了解决此问题的一种方法,那就是您将要搜索的字段带入一个额外的字段,其中包含可以生成的所有部分标记。 For instance if you have a field that contains the text search text then you will generate your tokens field containing s se sea sear searc e ea ear earc earch a ar arc arch r rc rch c ch t te tex e ex ext x xt . 例如,如果您有一个包含文本search text的字段,那么您将生成包含此s se sea sear searc e ea ear earc earch a ar arc arch r rc rch c ch t te tex e ex ext x xt tokens字段。 These will then all be treated as separate tokens. 这些都将被视为单独的令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM