问题
I want to query a document in Firestore using a regex. This is because in order to add a search functionality into my Flutter app, I need to be able to create a query based on the search. For example if the user types: 'alice' -- I would have a regex that would look like (?i)alice and I would query firestore for any documents that the name field meets this regex, whether it be Alice, Alice Doe, or Doe Alice. I'm looking for a query that would look something like this:
Firestore.instance.collection('people')
.where('name', matchesRegex: '(?i)alice')
Thanks for any help. I feel like this is a pretty basic feature but I can't find it any where in the docs. And I really can't get all of the documents and search based off of that because the collection is very large, and I don't want to read that many documents.
回答1:
Firestore does not offer any regular expression in queries. Those types of queries do not scale in the way that Firestore requires. It only supports exact string matches, and range queries with string prefixes.
If you need more flexible text-based queries, consider mirroring your data to a text search engine, such as Algola, as describe in the documentation.
来源:https://stackoverflow.com/questions/61602779/query-document-based-on-regex-in-firestore