1

Does Firebase allow for dynamic indexes for searches!?

I have the following structure:

PlayerInPool: {
      12345:                                 <- PoolId
          DUmwewIfzAbfWZN4NjS8mhX82 : true   <- UserId
          LpWgezRkC6EWS0sjXEWxhFl2  : true
          etc...              
    }
}

I put an observer as such:

ref.queryOrdered(byChild: (Auth.auth().currentUser?.uid)!).queryEqual(toValue: true).observe(.value, with: { snapshot in ...

When I run the app I get the following warning:

[Firebase/Database][I-RDB034028] Using an unspecified index. Consider adding ".indexOn": "DUmwewIfzAbfWZN4NjS8mhX82" at /PlayerInPool to your security rules for better performance

I am trying to do the following in the 'Rules' in Firebase

"PlayerInPool": {
  ".indexOn" : "auth.uid"
}

but it wont allow it stating that :

Error saving rules - Line 45: Invalid key: auth.uid. Index must be either .value or declared on a valid path

How can I get this to work!?

Updated Comment

If I invert the structure as suggested I would get this:

PlayerInPool: {
      DUmwewIfzAbfWZN4NjS8mhX82:        <- UserId                         
          12345 : true                  <- PoolId
          98765 : true
      LpWgezRkC6EWS0sjXEWxhFl2: 
          12345 : true
    }
}

I'm not entirely sure how this changes anything!?

Learn2Code
  • 1,974
  • 5
  • 24
  • 46
  • 1
    You cannot define dynamic indexes like that. Wanting to do so, typically means that you need to invert your data structure. For an example of that, see my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 15 '17 at 03:51
  • I read your response to the other question you answered but I do not fully understand. If I invert my structure then it would be users as the second level and the poolid with the 'dummy' true value!? I dont see how that changes anything? – Learn2Code Jun 15 '17 at 17:41
  • @FrankvanPuffelen how would I write the indexOn with the inverted structure? – Learn2Code Jun 15 '17 at 17:56
  • With the inverted structure you added, you can now find all pools for a player with a simple lookup: `/PlayerInPool/DUmwewIfzAbfWZN4NjS8mhX82`. You don't need an index for this, since you don't need to query: just a direct read. – Frank van Puffelen Jun 15 '17 at 17:59
  • @FrankvanPuffelen oh OK, now I understand it changes at the lookup in swift not on the Friebase rules; that is why I was wondering initially how this changes anything for the indexOn – Learn2Code Jun 15 '17 at 18:29
  • @FrankvanPuffelen quick question is the syntax ".indexOn": ["height"] or ".indexOn": "height" !? do I reallly need the square brackets [ ] ? – Learn2Code Jun 15 '17 at 18:33
  • Either will work. You need an array if you index on multiple properties. If you only index on a single property, you can either do a single value array or just the value. – Frank van Puffelen Jun 15 '17 at 19:24

0 Answers0