Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore warning on filtering with positional arguments. How to use 'filter' kwarg in Firestore queries?

Firestore started showing

UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.

when using query.where(field_path, op_string, value) while it's the the method from the official docs https://cloud.google.com/firestore/docs/query-data/queries

So how shall we use the 'filter' kwarg? Couldn't find docs or samples on that.

UPDATE: there is an open issue on this on GitHub https://github.com/googleapis/python-firestore/issues/705 (with no reaction from Google folks)

UPDATE2: so, basically, it should look like this

from google.cloud.firestore import CollectionReference
from google.cloud.firestore_v1.base_query import FieldFilter, BaseCompositeFilter

...

conditions = [[field, operator, value], [field, operator, value], ...]
query = CollectionReference(path1, path2, path3, ...)

query = query.where(filter=BaseCompositeFilter('AND', [FieldFilter(*_c) for _c in conditions]))

For a FieldFilter operator usual ==, !=, etc. work as specified here https://firebase.google.com/docs/firestore/query-data/queries#query_operators

Instead of 'AND' you could also use 'OPERATOR_UNSPECIFIED', but I'm not sure if it does what I think it should https://firebase.google.com/docs/firestore/reference/rest/v1/StructuredQuery#Operator

like image 817
syldman Avatar asked Sep 04 '25 04:09

syldman


2 Answers

Based on @Robert G's excellent answer you can get rid of this warning with a semi-small tweak.

Instead of:

query = collection_ref
query = query.where(field, op, value)

Do this:

from google.cloud.firestore_v1.base_query import FieldFilter
query = collection_ref
query = query.where(filter=FieldFilter(field, op, value))

And then the warning disappears. Hope this helps!

like image 194
Neil C. Obremski Avatar answered Sep 05 '25 20:09

Neil C. Obremski


As previously posted in the comments, there's an ongoing issue currently posted in Github. For those who experience similar issues, they could check this Github link for updates regarding the issue.

In addition, you could also file for a bug so that Google Devs/Engineers could also check on this and provide immediate solutions/fixes.

You could file a bug through these links:

  • Issue tracking system and product feature requests
  • Firebase bug report
like image 41
Robert G Avatar answered Sep 05 '25 18:09

Robert G