Skip to content

Commit 6e76a0e

Browse files
authored
[Cosmos] Remove GeoSpatial index limitation (#35790)
* Update README.md * add samples, add tests * Update index_management_async.py
1 parent 34180e4 commit 6e76a0e

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

sdk/cosmos/azure-cosmos/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ Streamable queries like `SELECT * FROM WHERE` *do* support continuation tokens.
164164
### Control Plane Limitations:
165165

166166
* Get CollectionSizeUsage, DatabaseUsage, and DocumentUsage metrics
167-
* Create Geospatial Index
168167
* Get the connection string
169168
* Get the minimum RU/s of a container
170169

sdk/cosmos/azure-cosmos/samples/index_management.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,54 @@ def perform_multi_orderby_query(db):
633633
print("Entity doesn't exist")
634634

635635

636+
def use_geospatial_indexing_policy(db):
637+
try:
638+
delete_container_if_exists(db, CONTAINER_ID)
639+
640+
# Create a container with vector embedding policy and vector indexes
641+
indexing_policy = {
642+
'includedPaths': [
643+
{'path': '/"Location"/?',
644+
'indexes': [
645+
{
646+
'kind': 'Spatial',
647+
'dataType': 'Point'
648+
}]
649+
},
650+
{
651+
'path': '/'
652+
}
653+
]
654+
}
655+
656+
created_container = db.create_container(
657+
id=CONTAINER_ID,
658+
partition_key=PARTITION_KEY,
659+
indexing_policy=indexing_policy
660+
)
661+
properties = created_container.read()
662+
print(created_container)
663+
664+
print("\n" + "-" * 25 + "\n9. Container created with geospatial indexes")
665+
print_dictionary_items(properties["indexingPolicy"])
666+
667+
# Create some items
668+
doc9 = created_container.create_item(body={"id": "loc1", 'Location': {'type': 'Point', 'coordinates': [20.0, 20.0]}})
669+
doc9 = created_container.create_item(body={"id": "loc2", 'Location': {'type': 'Point', 'coordinates': [100.0, 100.0]}})
670+
671+
# Run ST_DISTANCE queries using the geospatial index
672+
query = "SELECT * FROM root WHERE (ST_DISTANCE(root.Location, {type: 'Point', coordinates: [20.1, 20]}) < 20000)"
673+
query_documents_with_custom_query(created_container, query)
674+
675+
# Cleanup
676+
db.delete_container(created_container)
677+
print("\n")
678+
except exceptions.CosmosResourceExistsError:
679+
print("Entity already exists")
680+
except exceptions.CosmosResourceNotFoundError:
681+
print("Entity doesn't exist")
682+
683+
636684
def use_vector_embedding_policy(db):
637685
try:
638686
delete_container_if_exists(db, CONTAINER_ID)
@@ -663,7 +711,7 @@ def use_vector_embedding_policy(db):
663711
properties = created_container.read()
664712
print(created_container)
665713

666-
print("\n" + "-" * 25 + "\n9. Container created with vector embedding policy and vector indexes")
714+
print("\n" + "-" * 25 + "\n10. Container created with vector embedding policy and vector indexes")
667715
print_dictionary_items(properties["indexingPolicy"])
668716
print_dictionary_items(properties["vectorEmbeddingPolicy"])
669717

@@ -728,7 +776,10 @@ def run_sample():
728776
# 8. Perform Multi Orderby queries using composite indexes
729777
perform_multi_orderby_query(created_db)
730778

731-
# 9. Create and use a vector embedding policy
779+
# 9. Create and use a geospatial indexing policy
780+
use_geospatial_indexing_policy(created_db)
781+
782+
# 10. Create and use a vector embedding policy
732783
use_vector_embedding_policy(created_db)
733784

734785
except exceptions.AzureError as e:

sdk/cosmos/azure-cosmos/samples/index_management_async.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,54 @@ async def perform_multi_orderby_query(db):
628628
print("Entity doesn't exist")
629629

630630

631+
async def use_geospatial_indexing_policy(db):
632+
try:
633+
await delete_container_if_exists(db, CONTAINER_ID)
634+
635+
# Create a container with vector embedding policy and vector indexes
636+
indexing_policy = {
637+
'includedPaths': [
638+
{'path': '/"Location"/?',
639+
'indexes': [
640+
{
641+
'kind': 'Spatial',
642+
'dataType': 'Point'
643+
}]
644+
},
645+
{
646+
'path': '/'
647+
}
648+
]
649+
}
650+
651+
created_container = await db.create_container(
652+
id=CONTAINER_ID,
653+
partition_key=PARTITION_KEY,
654+
indexing_policy=indexing_policy
655+
)
656+
properties = await created_container.read()
657+
print(created_container)
658+
659+
print("\n" + "-" * 25 + "\n9. Container created with geospatial indexes")
660+
print_dictionary_items(properties["indexingPolicy"])
661+
662+
# Create some items
663+
doc9 = await created_container.create_item(body={"id": "loc1", 'Location': {'type': 'Point', 'coordinates': [20.0, 20.0]}})
664+
doc9 = await created_container.create_item(body={"id": "loc2", 'Location': {'type': 'Point', 'coordinates': [100.0, 100.0]}})
665+
666+
# Run ST_DISTANCE queries using the geospatial index
667+
query = "SELECT * FROM root WHERE (ST_DISTANCE(root.Location, {type: 'Point', coordinates: [20.1, 20]}) < 20000)"
668+
await query_documents_with_custom_query(created_container, query)
669+
670+
# Cleanup
671+
await db.delete_container(created_container)
672+
print("\n")
673+
except exceptions.CosmosResourceExistsError:
674+
print("Entity already exists")
675+
except exceptions.CosmosResourceNotFoundError:
676+
print("Entity doesn't exist")
677+
678+
631679
async def use_vector_embedding_policy(db):
632680
try:
633681
await delete_container_if_exists(db, CONTAINER_ID)
@@ -658,7 +706,7 @@ async def use_vector_embedding_policy(db):
658706
properties = await created_container.read()
659707
print(created_container)
660708

661-
print("\n" + "-" * 25 + "\n9. Container created with vector embedding policy and vector indexes")
709+
print("\n" + "-" * 25 + "\n10. Container created with vector embedding policy and vector indexes")
662710
print_dictionary_items(properties["indexingPolicy"])
663711
print_dictionary_items(properties["vectorEmbeddingPolicy"])
664712

@@ -723,7 +771,10 @@ async def run_sample():
723771
# 8. Perform Multi Orderby queries using composite indexes
724772
await perform_multi_orderby_query(created_db)
725773

726-
# 9. Create and use a vector embedding policy
774+
# 9. Create and use a geospatial indexing policy
775+
await use_geospatial_indexing_policy(created_db)
776+
777+
# 10. Create and use a vector embedding policy
727778
await use_vector_embedding_policy(created_db)
728779

729780
except exceptions.AzureError as e:

sdk/cosmos/azure-cosmos/test/test_crud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def test_document_upsert(self):
948948
before_create_documents_count,
949949
'number of documents should remain same')
950950

951-
def _test_spatial_index(self):
951+
def test_geospatial_index(self):
952952
db = self.databaseForTest
953953
# partial policy specified
954954
collection = db.create_container(

sdk/cosmos/azure-cosmos/test/test_crud_async.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,7 @@ async def test_document_upsert_async(self):
881881
document_list = [document async for document in created_collection.read_all_items()]
882882
assert len(document_list) == before_create_documents_count
883883

884-
async def _test_spatial_index(self):
885-
884+
async def test_geospatial_index_async(self):
886885
db = self.database_for_test
887886
# partial policy specified
888887
collection = await db.create_container(

0 commit comments

Comments
 (0)