问题
I was wondering if there was a recommended solution/library in Swift for integrating full text search (i.e. search bar that filters on presented data as you type, autocomplete)?
I am currently using Firestore as my backend.
Something I've glanced on is Algolia?
回答1:
You can use NSPredicate to search the text inside your objects, like this
let searchString = "test"
var arr:NSArray =
[["value" : "its a test text to find"],
["value" : "another text"],
["value" : "find this text"],
["value" : "lorem ipsum is a placeholder text commonly"],
["value" : "lorem ipsum is a"]]
var pre:NSPredicate = NSPredicate(format: "value CONTAINS[c] %@", searchString)
var result:NSArray = arr.filtered(using: pre) as NSArray
print(result)
it will return a array with the result based on the text that you search
回答2:
Algolia is probably the best solution right now, and is something Firebase themselves recommend. Just a few things to bear in mind is that you'll need a paid for Firebase plan to implement as you'll need a Cloud Function that sends data over to Algolia for indexing.
Once your data is in Algolia there is a great Swift library you can use to implement the Search Bars / Autocompletion you need.
来源:https://stackoverflow.com/questions/49667038/swift-full-text-search-recommended-solution