5

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.

Gabe
  • 5,643
  • 3
  • 26
  • 54
  • 1
    I have an app with a similar need. My approach is to build -- based on the name field, on a create trigger -- an object like this in the doc `searchTerms: { alice: true, doe:true }`, then query `.where('searchTerms.${searchTerm.toLowerCase()}', '==', true)` using backticks on the path string – danh May 04 '20 at 23:11
  • @danh , that is a good idea, I think there is a good chance I will implement this. – Gabe May 04 '20 at 23:23
  • @danh post your comment as a question and I will accept. – Gabe May 04 '20 at 23:25
  • The two users answering and closing this are pretty much the preeminent experts on Firebase, so if they disagree with some approach, I'd take their word for it. I added my 2 cents on the dup (which does a poor job answering this question, IMO). https://stackoverflow.com/questions/50769285/query-over-list-in-firebase – danh May 05 '20 at 15:02

1 Answers1

3

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • So than I how can I accomplish this search feature? – Gabe May 04 '20 at 23:03
  • With Firestore, you can't. You will need a different database. – Doug Stevenson May 04 '20 at 23:03
  • ok, algola looks nice, but i Dart is not listed as one of the supported languages, so how else could I implement this? Would the best way to be just write a cloud function that uses cloud function. I'm confused as to why this isn't supported because it seems like a pretty commonplace feature... – Gabe May 04 '20 at 23:07
  • One more question, how hard would it be to implement an indexing solution like this. I know little about how firestore indexes work so this might be a dumb question. – Gabe May 04 '20 at 23:24
  • 1
    Firebase has a feature now called Firebase Extensions. One such extension is "Search with Algolia" which "Enables full-text search of your Cloud Firestore data with Algolia." @Gabe – Othniel Nov 14 '22 at 15:59