我需要按相关性对搜索结果进行排序,同时尝试解决 Realm 的 NSPredicate 限制。
我当前的尝试重复结果:
if searchText.characters.count > 0 {
//First search is attempting exact match
relevantResults = Array(dataModel.terms.filter("%K BEGINSWITH[c] %@", "title", searchText).sorted(byProperty: "title"))
//Appended Results are looking for 'close enough' but include results that were already in the first search
relevantResults.append(contentsOf: Array(dataModel.terms.filter("%K CONTAINS[c] %@", "title", searchText).sorted(byProperty: "title")))
} else {
relevantResults = Array()
}
我尝试使用其他一些谓词过滤器发挥创意,例如:
"title NOT BEGINSWITH[c] %@ AND title CONTAINS[c] %@"
和
"SELF NOT IN %@"
Realm 不支持,或者谓词过滤器无效。 无论哪种方式,我都需要找到一个解决方案来按相关性排序而不重复。
在单独的 TableView 部分中将显示更多搜索结果,我也想排除“最佳匹配”结果。
如果可能的话,我想避免循环从第一组结果中获取所有标题,但似乎我可能不得不诉诸于此,除非有一些我没有考虑的数组函数?有什么建议吗?
最佳答案
显然我的谓词字符串有点不对劲。
"title CONTAINS[c] %@ AND NOT title BEGINSWITH[c] %@"
最终为我工作。
关于ios - 从 Realm 查询中排除对象 - 尝试按相关性排序(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39837908/