forked from typesense/typesense-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.py
More file actions
153 lines (124 loc) · 5.09 KB
/
collection.py
File metadata and controls
153 lines (124 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
This module provides functionality for managing individual collections in the Typesense API.
It contains the Collection class, which allows for retrieving, updating, and deleting
collections, as well as managing documents, overrides, and synonyms within a collection.
Classes:
Collection: Manages operations on a single collection in the Typesense API.
Dependencies:
- typesense.api_call: Provides the ApiCall class for making API requests.
- typesense.documents: Provides the Documents class for managing documents.
- typesense.overrides: Provides the Overrides class for managing overrides.
- typesense.synonyms: Provides the Synonyms class for managing synonyms.
- typesense.types.collection: Provides CollectionSchema and CollectionUpdateSchema types.
- typesense.types.document: Provides DocumentSchema type.
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
"""
import sys
from typing_extensions import deprecated
from typesense.types.collection import CollectionSchema, CollectionUpdateSchema
if sys.version_info >= (3, 11):
import typing
else:
import typing_extensions as typing
from typesense.api_call import ApiCall
from typesense.documents import Documents
from typesense.overrides import Overrides
from typesense.synonyms import Synonyms
from typesense.types.document import DocumentSchema
TDoc = typing.TypeVar("TDoc", bound=DocumentSchema)
class Collection(typing.Generic[TDoc]):
"""
Manages operations on a single collection in the Typesense API.
This class provides methods to retrieve, update, and delete a collection,
as well as access to documents, overrides, and synonyms within the collection.
It is generic over the document type TDoc, which should be a subtype of DocumentSchema.
Attributes:
name (str): The name of the collection.
api_call (ApiCall): The ApiCall instance for making API requests.
documents (Documents[TDoc]): Instance for managing documents in this collection.
overrides (Overrides): Instance for managing overrides in this collection.
synonyms (Synonyms): Instance for managing synonyms in this collection.
"""
def __init__(self, api_call: ApiCall, name: str):
"""
Initialize the Collection instance.
Args:
api_call (ApiCall): The ApiCall instance for making API requests.
name (str): The name of the collection.
"""
self.name = name
self.api_call = api_call
self.documents: Documents[TDoc] = Documents(api_call, name)
self._overrides = Overrides(api_call, name)
self._synonyms = Synonyms(api_call, name)
@property
@deprecated(
"Synonyms is deprecated on v30+. Use client.synonym_sets instead.",
category=None,
)
def synonyms(self) -> Synonyms:
return self._synonyms
@property
@deprecated(
"Overrides is deprecated on v30+. Use client.curation_sets instead.",
category=None,
)
def overrides(self) -> Overrides:
return self._overrides
def retrieve(self) -> CollectionSchema:
"""
Retrieve the schema of this collection from Typesense.
Returns:
CollectionSchema: The schema of the collection.
"""
response: CollectionSchema = self.api_call.get(
endpoint=self._endpoint_path,
entity_type=CollectionSchema,
as_json=True,
)
return response
def update(self, schema_change: CollectionUpdateSchema) -> CollectionUpdateSchema:
"""
Update the schema of this collection in Typesense.
Args:
schema_change (CollectionUpdateSchema):
The changes to apply to the collection schema.
Returns:
CollectionUpdateSchema: The updated schema of the collection.
"""
response: CollectionUpdateSchema = self.api_call.patch(
endpoint=self._endpoint_path,
body=schema_change,
entity_type=CollectionUpdateSchema,
)
return response
def delete(
self,
delete_parameters: typing.Union[
typing.Dict[str, typing.Union[str, bool]],
None,
] = None,
) -> CollectionSchema:
"""
Delete this collection from Typesense.
Args:
delete_parameters (Union[Dict[str, Union[str, bool]], None], optional):
Additional parameters for the delete operation. Defaults to None.
Returns:
CollectionSchema: The schema of the deleted collection.
"""
response: CollectionSchema = self.api_call.delete(
self._endpoint_path,
entity_type=CollectionSchema,
params=delete_parameters,
)
return response
@property
def _endpoint_path(self) -> str:
"""
Get the API endpoint path for this collection.
Returns:
str: The full endpoint path for the collection.
"""
from typesense.collections import Collections
return "/".join([Collections.resource_path, self.name])