使用 Firebase 实时数据库运行我的 Android 应用时,我收到以下警告:
Using an unspecified index. Consider adding ".indexOn" ... to your security and Firebase Database rules for better performance
我完全理解这个警告。但我不知道如何让它变得更好。我真的只想查询索引字段!
这是我的数据库:
{
"groupUsers" : {
"g1" : {
"u1" : "admin"
},
"g2" : {
"u1" : "admin",
"u2" : "readonly"
}
},
"groups" : {
"g1" : {
"areas" : {
"a1" : {
"groupId" : "g1",
"name" : "My Group"
}
},
"interests" : {
"i1" : {
"groupId" : "g1",
"name" : "My Interest"
}
},
"points" : {
"p1" : {
"address" : "First Street",
"areaId" : "a1",
"groupId" : "g1",
"latitude" : -25,
"longitude" : -55,
"name" : "Harry"
}
},
"properties" : {
"name" : "My Group Name"
},
"waypoints" : {
"w1" : {
"areaId" : "a1",
"groupId" : "g1"
}
}
}
}
"users" : {
"u1" : {
"email" : "some@domain.com",
"firstName" : "Peter",
"lastName" : "Smith"
},
"u2" : {
"email" : "other@email.com",
"firstName" : "John",
"lastName" : "Wayne"
}
}
}
这些是我的安全规则:
{
"rules": {
"groups": {
"$groupId": {
".read": "root.child('groupUsers').child($groupId).child(auth.uid).exists()",
".write": "! root.child('groupUsers').child($groupId).exists() || root.child('groupUsers').child($groupId).child(auth.uid).val() === 'admin'",
"$child": {
".write": "root.child('groupUsers').child($groupId).child(auth.uid).exists() && root.child('groupUsers').child($groupId).child(auth.uid).val() !== 'readonly' && ($child === 'points' || $child === 'visits')"
}
},
"areas": {
".indexOn": ["groupId", "name"]
},
"waypoints": {
".indexOn": ["groupId", "areaId", "sequenceNumber"]
},
"interests": {
".indexOn": ["groupId", "rank", "name"]
},
"points": {
".indexOn": ["groupId", "areaId", "name"]
},
"visits": {
".indexOn": ["groupId", "pointId", "interestId", "userId"]
}
},
"users": {
".read": "auth != null",
"$userId": {
".write": "auth != null && $userId === auth.uid && newData.val() != null",
".indexOn": ["email", "firstName", "lastName"]
}
},
"groupUsers": {
".read": "auth != null",
"$groupId": {
".write": "auth != null && (root.child('groupUsers').child($groupId).child(auth.uid).val() === 'admin' || !root.child('groupUsers').child($groupId).exists())"
}
}
}
}
问题在于 groupUser
结构。它具有组键作为属性名称。 我没有要索引的字段,因为我没有常量属性名称。
如何更改结构以使所有字段都被索引并且我的所有规则仍然有效?
最佳答案
试着回答你的问题,我明白你的意思是你没有设置索引的键名,根据 documentation here我们可以在这里使用 ".value"
来添加 index on values,
下面可能对您有用,在 groupUsers 的用户角色级别添加了 .value
,因为这似乎是仅字符串值(用户角色) 可以设置索引的地方
"groupUsers": {
".read": "auth != null",
"$groupId": {
".write": "auth != null && (root.child('groupUsers').child($groupId).child(auth.uid).val() === 'admin' || !root.child('groupUsers').child($groupId).exists())"
".indexOn": ".value"
}
}
关于java - Firebase DB 警告 : "Using an unspecified index. Consider adding ' ". indexOn"以获得更好的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48128902/