如何在CloudKit记录中搜索部分单词? [英] How do I search for a partial word in a CloudKit record?

查看:47
本文介绍了如何在CloudKit记录中搜索部分单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索包含用户输入的搜索文本的CloudKit记录.

I'm trying to search for CloudKit records containing search text entered by my users.

我有这些标题的记录(字符串格式):

I have records with these titles (in String format):

  • 第1项
  • 第2项
  • 第3项
  • 测试1
  • 测试2

我尝试使用包含":

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.如果我搜索项目",则会返回项目1,项目2和项目3,但是如果我搜索"tem",则不会得到任何结果.

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.

是否可以在搜索中包含通配符或使用部分搜索文本的另一种方式?

Is there a way of including a wildcard character in the search or another way of using partial search text?

我也尝试过:

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

但结果相同

推荐答案

您只能从字段开头搜索部分文本.例如,您可以使用:

You can only search for partial texts in from the beginning of a field. For instance you could use:

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

令牌匹配查询在您的记录中搜索令牌(单词).它将始终搜索完整的单词.到目前为止,我只找到了解决此问题的一种方法,那就是您将要搜索的字段带入一个额外的字段,其中包含可以生成的所有部分标记.例如,如果您有一个包含文本 search text 的字段,那么您将生成包含 se sear sear searc e earar ear aar arch arch r rc rch c ch的令牌字段.外部扩展名xcode .这些都将被视为单独的令牌.

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. These will then all be treated as separate tokens.

这篇关于如何在CloudKit记录中搜索部分单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆