HoRiz
HoRiz

Reputation: 787

Handling Multiple Optional Conditions in Firestore Queries - flutter

I want to get documents from Firestore with conditions. There are 5 conditions where 4 of them are optional. It depends on user input (the user can enter conditions. depending on the user.)

My problem is that I have to create indexes in Firestore. If I use 5 conditions all time, It's enough 1 index. But, In this situation, Some users use 4 conditions, and someone uses 2,3,1. I don't know how many. So, I have to create many indexes. (more than 20). What can I do about this?

I have an idea for it. All conditions work every time (1 index), But, If the user does not give value to that condition, That condition give all documents. So, I used this method to success my idea. But there is errors. help me to improve my idea or give me another idea to do this.

Code for my idea:

profession, religion, status etc. values get from user inputs.

Stream<QuerySnapshot> getDataStream(
    String religion,
    String status,
    String profession,
    String foods,
    String education,
  ) {
    var query = FirebaseFirestore.instance
        .collection("users")
        .where("ethanicity", isEqualTo: "Test");

    // religion
    if (religion != "") {
      query = query.where("religion", isEqualTo: religion);
    }
    if (religion == "") {
      query = query.where("religion", isNotEqualTo: "");
    }

    // Status
    if (status != "") {
      query = query.where("status", isEqualTo: status);
    }
    if (status == "") {
      query = query.where("status", isNotEqualTo: "");
    }

    // // profession
    if (profession != "") {
      query = query.where("profession", isEqualTo: profession);
    }
    if (profession == "") {
      query = query.where("profession", isNotEqualTo: "");
    }

    // // foods
    if (foods != "") {
      query = query.where("status", isEqualTo: foods);
    }
    if (foods == "") {
      query = query.where("status", isNotEqualTo: "");
    }

    //education
    if (education != "") {
      query = query.where("education", isEqualTo: education);
    } else {
      query = query.where("education", isNotEqualTo: "");
    }

    return query.snapshots();
  }

Problem : I can use isNotEqualTo multiple times in a single query. Can't use like this.

Upvotes: 0

Views: 589

Answers (1)

alexalejandroem
alexalejandroem

Reputation: 1162

I know two ways to do this, the scalable and the not so scalable version.

Scalable: Use Algolia plugin as an external service to search, it's worth the time learning as you will have a bunch of power for queries.

The other solution is as per https://firebase.google.com/docs/firestore/query-data/queries#limitations firebase limitation, you can only filter on the same field.

You could create an array field called filters ["status", "religion"] and from there you can use a where in query. Now, this is hard to escalate as you would have to update such an array every time a property changes.

Upvotes: 1

Related Questions