Skip to content

Commit

Permalink
Add sample queries for optimizing-indexes docs (#2855)
Browse files Browse the repository at this point in the history
* Add sample queries for optimizing-indexes docs

* Fix syntax error
  • Loading branch information
jlara310 authored Feb 21, 2020
1 parent e75317a commit d1a05eb
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
91 changes: 91 additions & 0 deletions datastore/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,97 @@ def eventual_consistent_query(client):
pass


def index_merge_queries(client):
# Create a Photo entity to query
photo = datastore.Entity(client.key('Photo', 'sample_photo'))

photo.update({
'owner_id': 'user1234',
'size': 2,
'coloration': 2,
'tag': ['family', 'outside', 'camping']
})

client.put(photo)

# Sample queries using built-in indexes
queries = []

# [START datastore_built_in_index_queries]
query_owner_id = client.query(
kind='Photo',
filters=[('owner_id', '=', 'user1234')])

query_size = client.query(
kind='Photo',
filters=[('size', '=', 2)])

query_coloration = client.query(
kind='Photo',
filters=[('coloration', '=', 2)])
# [END datastore_built_in_index_queries]

queries.append(query_owner_id)
queries.append(query_size)
queries.append(query_coloration)

# [START datastore_merged_index_query]
query_all_properties = client.query(
kind='Photo',
filters=[('owner_id', '=', 'user1234'),
('size', '=', 2),
('coloration', '=', 2),
('tag', '=', 'family')])
# [END datastore_merged_index_query]

queries.append(query_all_properties)

# [START datastore_merged_index_tag_queries]
query_tag = client.query(
kind='Photo',
filters=[('tag', '=', 'family'),
('tag', '=', 'outside'),
('tag', '=', 'camping')])

query_owner_size_color_tags = client.query(
kind='Photo',
filters=[('owner_id', '=', 'user1234'),
('size', '=', 2),
('coloration', '=', 2),
('tag', '=', 'family'),
('tag', '=', 'outside'),
('tag', '=', 'camping')])
# [END datastore_merged_index_tag_queries]

queries.append(query_tag)
queries.append(query_owner_size_color_tags)

# [START datastore_owner_size_tag_query]
query_owner_size_tag = client.query(
kind='Photo',
filters=[('owner_id', '=', 'username'),
('size', '=', 2),
('tag', '=', 'family')])
# [END datastore_owner_size_tag_query]

queries.append(query_owner_size_tag)

# [START datastore_size_coloration_query]
query_size_coloration = client.query(
kind='Photo',
filters=[('size', '=', 2),
('coloration', '=', 1)])
# [END datastore_size_coloration_query]

queries.append(query_size_coloration)

results = []
for query in queries:
results.append(query.fetch())

return results


def main(project_id):
client = datastore.Client(project_id)

Expand Down
4 changes: 4 additions & 0 deletions datastore/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,7 @@ def test_property_by_kind_run_query(self, client):
client.entities_to_delete.extend(
client.query(kind='Task').fetch())
assert reprs

@eventually_consistent.mark
def test_index_merge_queries(self, client):
snippets.index_merge_queries(client)

0 comments on commit d1a05eb

Please sign in to comment.