Tuesday, 26 September 2023

firebase generic filtering needs indexes for each combination

i have the following code to query properties (paginated) with filters:

const propertyCollectionRef = db.collection('properties')

    let queryRef = propertyCollectionRef;

    let query: any = db.collection('properties');

    if (filters) {
        for (const [key, value] of Object.entries(filters)) {
            if (key !== 'page') {
                switch (key) {
                    case "price":
                        query = query.where(key, '<=', parseInt(value));
                        break;
                    case "bathrooms":
                        query = query.where(key, '==', parseInt(value));
                        break;
                    case "beds":
                        query = query.where(key, '==', parseInt(value));
                        break;
                    case "min-area":
                        query = query.where("area", '>=', parseInt(value));
                        break;
                    case "max-area":
                        query = query.where("area", '<=', parseInt(value));
                        break;
                    case "typ":
                        query = query.where(key, '==', value);
                        break;
                    default:
                        query = query.where(key, '==', value);
                        break;
                }
            }
        }
    }



    const offset = (page - 1) * pageSize

    const querySnapshot = await query
        .limit(pageSize)
        .offset(offset).get();

firebase wants me to create an index for each query combination (

code: 9,
  details: 'The query requires an index. You can create it here:...

) made when choosing different filters, for example i need to create one for filtering price and typ. but also one when filtering for price, typ and bathrooms

how is it possible to make this more "generic" - i can't think of firebase to make it such complicated...



from firebase generic filtering needs indexes for each combination

No comments:

Post a Comment