Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1a05eb

Browse filesBrowse files
authored
Add sample queries for optimizing-indexes docs (GoogleCloudPlatform#2855)
* Add sample queries for optimizing-indexes docs * Fix syntax error
1 parent e75317a commit d1a05eb
Copy full SHA for d1a05eb

File tree

Expand file treeCollapse file tree

2 files changed

+95
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+95
-0
lines changed

‎datastore/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: datastore/cloud-client/snippets.py
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,97 @@ def eventual_consistent_query(client):
789789
pass
790790

791791

792+
def index_merge_queries(client):
793+
# Create a Photo entity to query
794+
photo = datastore.Entity(client.key('Photo', 'sample_photo'))
795+
796+
photo.update({
797+
'owner_id': 'user1234',
798+
'size': 2,
799+
'coloration': 2,
800+
'tag': ['family', 'outside', 'camping']
801+
})
802+
803+
client.put(photo)
804+
805+
# Sample queries using built-in indexes
806+
queries = []
807+
808+
# [START datastore_built_in_index_queries]
809+
query_owner_id = client.query(
810+
kind='Photo',
811+
filters=[('owner_id', '=', 'user1234')])
812+
813+
query_size = client.query(
814+
kind='Photo',
815+
filters=[('size', '=', 2)])
816+
817+
query_coloration = client.query(
818+
kind='Photo',
819+
filters=[('coloration', '=', 2)])
820+
# [END datastore_built_in_index_queries]
821+
822+
queries.append(query_owner_id)
823+
queries.append(query_size)
824+
queries.append(query_coloration)
825+
826+
# [START datastore_merged_index_query]
827+
query_all_properties = client.query(
828+
kind='Photo',
829+
filters=[('owner_id', '=', 'user1234'),
830+
('size', '=', 2),
831+
('coloration', '=', 2),
832+
('tag', '=', 'family')])
833+
# [END datastore_merged_index_query]
834+
835+
queries.append(query_all_properties)
836+
837+
# [START datastore_merged_index_tag_queries]
838+
query_tag = client.query(
839+
kind='Photo',
840+
filters=[('tag', '=', 'family'),
841+
('tag', '=', 'outside'),
842+
('tag', '=', 'camping')])
843+
844+
query_owner_size_color_tags = client.query(
845+
kind='Photo',
846+
filters=[('owner_id', '=', 'user1234'),
847+
('size', '=', 2),
848+
('coloration', '=', 2),
849+
('tag', '=', 'family'),
850+
('tag', '=', 'outside'),
851+
('tag', '=', 'camping')])
852+
# [END datastore_merged_index_tag_queries]
853+
854+
queries.append(query_tag)
855+
queries.append(query_owner_size_color_tags)
856+
857+
# [START datastore_owner_size_tag_query]
858+
query_owner_size_tag = client.query(
859+
kind='Photo',
860+
filters=[('owner_id', '=', 'username'),
861+
('size', '=', 2),
862+
('tag', '=', 'family')])
863+
# [END datastore_owner_size_tag_query]
864+
865+
queries.append(query_owner_size_tag)
866+
867+
# [START datastore_size_coloration_query]
868+
query_size_coloration = client.query(
869+
kind='Photo',
870+
filters=[('size', '=', 2),
871+
('coloration', '=', 1)])
872+
# [END datastore_size_coloration_query]
873+
874+
queries.append(query_size_coloration)
875+
876+
results = []
877+
for query in queries:
878+
results.append(query.fetch())
879+
880+
return results
881+
882+
792883
def main(project_id):
793884
client = datastore.Client(project_id)
794885

‎datastore/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: datastore/cloud-client/snippets_test.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,7 @@ def test_property_by_kind_run_query(self, client):
276276
client.entities_to_delete.extend(
277277
client.query(kind='Task').fetch())
278278
assert reprs
279+
280+
@eventually_consistent.mark
281+
def test_index_merge_queries(self, client):
282+
snippets.index_merge_queries(client)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.