在Firestore中基于正则表达式查询文档 [英] Query Document Based on Regex in Firestore
问题描述
我想使用正则表达式在Firestore中查询文档.这是因为要将搜索功能添加到我的Flutter应用中,我需要能够基于搜索创建查询.例如,如果用户键入:'alice'-我将拥有一个看起来像(?i)alice
的正则表达式,并且我将在firestore中查询名称字段符合此正则表达式的任何文档,无论它是Alice,Alice Doe还是Doe爱丽丝我正在寻找一个看起来像这样的查询:
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.
推荐答案
Firestore在查询中不提供任何正则表达式.这些类型的查询无法按照Firestore的要求进行扩展.它仅支持完全匹配的字符串以及带有字符串前缀的范围查询.
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.
如果您需要更灵活的基于文本的查询,请考虑将数据镜像到文本搜索引擎(例如Algola),如文档.
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.
这篇关于在Firestore中基于正则表达式查询文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!