2

It seems there is no way to construct query with OR condition. Has anyone hit this issue or know when this will be done or any workaround.

What I want to achive something like this with OR:

query = datastore.query(kind='Article',
                         filters=[('url', '=', 'url1'),
                                  ('url', '=', 'url2')]
                       )

But this filter works as AND not OR.

bossylobster
  • 9,993
  • 1
  • 42
  • 61
Mohammad Haque
  • 565
  • 1
  • 7
  • 16

2 Answers2

3

OR is not a supported query construct in Google Cloud Datastore.

The current way to achieve this is to construct multiple queries client-side and combine the result sets.

For reference, you should read through the Datastore Queries documentation:

The Datastore currently only supports combining filters with the AND operator. However it's relatively straightforward to create your own OR query by issuing multiple queries and combining the results:

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
1

Python runtime supports "IN" query filter.

Note, however, that this is just a convenience: under the hood, "IN" query is translated into a series of independent queries each looking for one value on the list.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • I don't see 'IN' in gcloud python library. Can you point me in gcloud doc if it does exist in there. – Mohammad Haque Jan 31 '16 at 18:51
  • Sorry, I was not probably clear enough, I'm trying to use gcloud python library which is different than general python datastore implementation. https://googlecloudplatform.github.io/gcloud-python/stable/datastore-entities.html – Mohammad Haque Jan 31 '16 at 19:40
  • I would say the reason `AND` is supported but `OR` isn't is that *most* Datastore queries are index scans so an `AND` query is a scan of a *custom* index, whereas an `OR` query can be achieved by multiple queries (scans) of a simple index. As @AndreiVolgin says, `IN` is syntactic sugar. – tx802 Jan 31 '16 at 19:45