Skip to content

Firestore compound range query with missing index (no warning, double callback) #18948

Description

@ashtonthomas

Label: Firestore Plugin

Desired Outcome:

  • Show warning when missing a compound index
  • This situation should not result in a double callback (once with data, once without)

Possibly relevant

I was performing the range query on a String field assuming lexicographical ordering (also, the string is of a bastardized date e.g. "2018-06-29")

Background

Using the Flutter Cloud Firestore Plugin, I have run into the 'compound query' issue.

However, to combine the equality operator (==) with a range comparison (<, <=, >, or >=), make sure to create a custom index.

This messages take you to the Managing indexes in Cloud Firestore docs:

If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

In my case, I saw no such error message or link

My issue

I naively ran into this issue but was confused when I was seeing the handler callback (listening to the query snapshot) was being fired twice. Even stranger is that it first first with the correct data (well, what I assume to be the correct data) and then a second time with an empty list. Thus my flutter application gets a flash of data then poof!

This takes us back to the expected outcome of:

  • Can we show the compound index warnings with link to create the correct index
  • What's up with the double callback once with the data and then without?

Once I added the compound query (manually on the two fields) everything worked as expected and the handler function was only called once per update.

Below is basically my query (I'm including the wonky date as a string details rather than concealing this terrible truth from the world):

static StreamSubscription streamFooIdsForBar(String barId,
      String startDateStr, String endDateStr, onData(List<String> fooIds)) {
    return Firestore.instance
        .collection(SOME_COLLECTION_CONST)
        // equality clause on barId
        .where(
          'barId',
          isEqualTo: barId,
        )
        
        // range clauses on a funky date as string...
        .where(
          'someDateAsString',
          isGreaterThanOrEqualTo: startDateStr,
        )
        .where(
          'someDateAsString',
          isLessThanOrEqualTo: endDateStr,
        )
        .snapshots()
        .listen((QuerySnapshot query) {

      onData(query.documents
          .map((DocumentSnapshot doc) => doc.documentID)
          .toList());

    });
  }

Notes

flutter analyze -> nothing

flutter doctor -v

[✓] Flutter (Channel master, v0.5.6-pre.61, on Mac OS X 10.13.5 17F77, locale en-US)
    • Flutter version 0.5.6-pre.61 at /Users/ashton/development/flutter/flutter
    • Framework revision 2e559584e2 (7 days ago), 2018-06-22 16:32:32 -0700
    • Engine revision 549c855e89
    • Dart version 2.0.0-dev.63.0.flutter-4c9689c1d2

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at /Users/ashton/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.4.1, Build version 9F2000
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.0

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 25.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] VS Code (version 1.24.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.14.0

[✓] Connected devices (1 available)
    • iPhone 5s • 139775FA-03A3-4426-A335-C89AAF2BFA87 • ios • iOS 11.4 (simulator)

• No issues found!

Activity

  1. pulyaevskiy commented on Sep 4, 2018

    @pulyaevskiy
    Contributor

    This is a pretty annoying issue.

    I did find a workaround though. Query.getDocuments() actually fails with an error which contains link for the custom index. Still if you don't know much about indexes it would be hard to discover this problem.

  2. ashtonthomas commented on Sep 4, 2018

    @ashtonthomas
    Author

    @pulyaevskiy, ah so calling getDocuments() rather than adding a snapshot listeners gives you the correct index link?

    Yeah, my worry is that this will leave a lot of people scratching their heads

  3. pulyaevskiy commented on Sep 4, 2018

    @pulyaevskiy
    Contributor

    calling getDocuments() rather than adding a snapshot listeners gives you the correct index link?

    Correct. It works this way in my tests at least.

  4. kroikie commented on Oct 13, 2019

    @kroikie

    @ashtonthomas

    This issue has been moved to firebase/flutterfire#774. Any further collaboration will be done there.

  5. github-actions commented on Aug 27, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Aug 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p: cloud_firestoreObsolete - Firebase Firestore pluginp: firebaseObsolete - Firebase pluginspackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions