99
1010import attr
1111from opensearchpy import exceptions , helpers
12- from opensearchpy .exceptions import TransportError
1312from opensearchpy .helpers .query import Q
1413from opensearchpy .helpers .search import Search
1514from starlette .requests import Request
@@ -80,24 +79,21 @@ async def create_collection_index() -> None:
8079 """
8180 client = AsyncSearchSettings ().create_client
8281
83- search_body : Dict [str , Any ] = {
84- "aliases" : {COLLECTIONS_INDEX : {}},
85- }
86-
8782 index = f"{ COLLECTIONS_INDEX } -000001"
8883
89- try :
90- await client .indices .create (index = index , body = search_body )
91- except TransportError as e :
92- if e .status_code == 400 :
93- pass # Ignore 400 status codes
94- else :
95- raise e
96-
84+ exists = await client .indices .exists (index = index )
85+ if not exists :
86+ await client .indices .create (
87+ index = index ,
88+ body = {
89+ "aliases" : {COLLECTIONS_INDEX : {}},
90+ "mappings" : ES_COLLECTIONS_MAPPINGS ,
91+ },
92+ )
9793 await client .close ()
9894
9995
100- async def create_item_index (collection_id : str ):
96+ async def create_item_index (collection_id : str ) -> None :
10197 """
10298 Create the index for Items. The settings of the index template will be used implicitly.
10399
@@ -109,24 +105,22 @@ async def create_item_index(collection_id: str):
109105
110106 """
111107 client = AsyncSearchSettings ().create_client
112- search_body : Dict [str , Any ] = {
113- "aliases" : {index_alias_by_collection_id (collection_id ): {}},
114- }
115108
116- try :
109+ index_name = f"{ index_by_collection_id (collection_id )} -000001"
110+ exists = await client .indices .exists (index = index_name )
111+ if not exists :
117112 await client .indices .create (
118- index = f"{ index_by_collection_id (collection_id )} -000001" , body = search_body
113+ index = index_name ,
114+ body = {
115+ "aliases" : {index_alias_by_collection_id (collection_id ): {}},
116+ "mappings" : ES_ITEMS_MAPPINGS ,
117+ "settings" : ES_ITEMS_SETTINGS ,
118+ },
119119 )
120- except TransportError as e :
121- if e .status_code == 400 :
122- pass # Ignore 400 status codes
123- else :
124- raise e
125-
126120 await client .close ()
127121
128122
129- async def delete_item_index (collection_id : str ):
123+ async def delete_item_index (collection_id : str ) -> None :
130124 """Delete the index for items in a collection.
131125
132126 Args:
0 commit comments