Firebase Firestore Query Index Issue with Multiple Filters and Combinations

I am building a website that uses pagination and filtering of some data.

if (eventType) query = query.where('general.eventType', '==', eventType); 
if (price) query = query.where('general.eventTicketPrice', price == "free" ? '==' : '>=', 0);
if (startDate) query = query.where('general.eventDates.start', '>=', new Date(startDate));
if (endDate) query = query.where('general.eventDates.start', '<=', new Date(endDate));
if (location) query = query.where('general.location.city', '==', location);
if (participation) query = query.where('general.locationType', '==', participation);
...
...

I have about 20 filters that I use, and whenever I select any filter the firebase needs me to create a new index. I am assuming that I need to do this for each combination of filters. I basically made a combination generator that tries each query and gives me the URL for creating the index, but there are A LOT of them. Like thousands so naturally I don’t want to generate them manually.

Is there a way to fix this issue?

I thought about getting all the documents and then filtering manually. It would be easy to implement but may be costly and maybe high latency (idk). Currently, there are not many documents but I want to be able to query hundreds maybe thousands of documents.

What are some recommendations for this type of issue?

Recognized by Google Cloud Collective

0

A general rule for Firestore is that it every query is always executed against one or more indexes, but that doesn’t mean you need a separate index for every query. From the documentation on index merging:

Although Cloud Firestore uses an index for every query, it doesn’t necessarily require one index per query. For queries with multiple equality (==) clauses and, optionally, an orderBy clause, Cloud Firestore can re-use existing indexes. Cloud Firestore can merge the indexes for simple equality filters to build the composite indexes needed for larger equality queries.

In your case, you might need to move your relational conditions (>=, <=, etc) to the end of the query, but could then potentially depend on the existing single field indexes for most equality checks.

Note: queries that use merged indexes will typically perform slower than if you’d create a dedicated composite index for them. So you’ll want to keep an eye on query performance, and optimize as needed.

I actually recorded a short video about an instance where this happened during Google I/O last year, which you can see here.

Recognized by Google Cloud Collective

Not clear on what is broken in your case, but generally speaking:

Cloud Firestore ensures query performance by requiring an index for
every query. The indexes required for the most basic queries are
automatically created for you
.

https://firebase.google.com/docs/firestore/query-data/indexing

Then at https://firebase.google.com/docs/firestore/query-data/queries#query_limitations …

Query limitations

The following list summarizes Cloud Firestore query limitations:

  • Cloud Firestore provides support for logical OR queries through the or, in, and array-contains-any operators. These queries are limited to
    30 disjunctions based on the query’s disjunctive normal form.
  • You can use at most one array-contains clause per disjunction (or group). You can’t combine array-contains with array-contains-any in
    the same disjunction.
  • You can’t combine not-in with in, array-contains-any, or or in the same query.
  • Only a single not-in or != is allowed per query. not-in supports up to 10 comparison values.
  • The sum of filters, sort orders, and parent document path (1 for a subcollection, 0 for a root collection) in a query cannot exceed 100.
    This is calculated based on the disjunctive normal form of the query.
  • A query with an inequality filter on a field implies ordering by that field and filters for existence of that field.

Limits on OR queries

To prevent a query from becoming too computationally expensive, Cloud Firestore limits how many AND and OR
clauses you can combine. To apply this limit, Cloud Firestore converts
queries that perform logical OR operations (or, in, and
array-contains-any) to disjunctive normal form (also known as an OR of
ANDs). Cloud Firestore limits a query to a maximum of 30
disjunctions in disjunctive normal form
.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật