qdrant_client.py 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163
  1. import warnings
  2. from typing import (
  3. Any,
  4. Awaitable,
  5. Callable,
  6. Iterable,
  7. Mapping,
  8. Optional,
  9. Sequence,
  10. Union,
  11. )
  12. import numpy as np
  13. from qdrant_client import grpc as grpc
  14. from qdrant_client.client_base import QdrantBase
  15. from qdrant_client.common.client_warnings import show_warning_once
  16. from qdrant_client.conversions import common_types as types
  17. from qdrant_client.embed.type_inspector import Inspector
  18. from qdrant_client.http import ApiClient, SyncApis
  19. from qdrant_client.local.qdrant_local import QdrantLocal
  20. from qdrant_client.migrate import migrate
  21. from qdrant_client.qdrant_fastembed import QdrantFastembedMixin
  22. from qdrant_client.qdrant_remote import QdrantRemote
  23. class QdrantClient(QdrantFastembedMixin):
  24. """Entry point to communicate with Qdrant service via REST or gRPC API.
  25. It combines interface classes and endpoint implementation.
  26. Additionally, it provides custom implementations for frequently used methods like initial collection upload.
  27. All methods in QdrantClient accept both gRPC and REST structures as an input.
  28. Conversion will be performed automatically.
  29. .. note::
  30. This module methods are wrappers around generated client code for gRPC and REST methods.
  31. If you need lower-level access to generated clients, use following properties:
  32. - :py:attr:`QdrantClient.grpc_points`
  33. - :py:attr:`QdrantClient.grpc_collections`
  34. - :py:attr:`QdrantClient.rest`
  35. .. note::
  36. If you need async, please consider using Async Implementations of QdrantClient.
  37. - :class:`qdrant_client.async_qdrant_client`
  38. Args:
  39. location:
  40. If `":memory:"` - use in-memory Qdrant instance.
  41. If `str` - use it as a `url` parameter.
  42. If `None` - use default values for `host` and `port`.
  43. url: either host or str of "Optional[scheme], host, Optional[port], Optional[prefix]".
  44. Default: `None`
  45. port: Port of the REST API interface. Default: 6333
  46. grpc_port: Port of the gRPC interface. Default: 6334
  47. prefer_grpc: If `true` - use gPRC interface whenever possible in custom methods.
  48. https: If `true` - use HTTPS(SSL) protocol. Default: `None`
  49. api_key: API key for authentication in Qdrant Cloud. Default: `None`
  50. prefix:
  51. If not `None` - add `prefix` to the REST URL path.
  52. Example: `service/v1` will result in `http://localhost:6333/service/v1/{qdrant-endpoint}` for REST API.
  53. Default: `None`
  54. timeout:
  55. Timeout for REST and gRPC API requests.
  56. Default: 5 seconds for REST and unlimited for gRPC
  57. host: Host name of Qdrant service. If url and host are None, set to 'localhost'.
  58. Default: `None`
  59. path: Persistence path for QdrantLocal. Default: `None`
  60. force_disable_check_same_thread:
  61. For QdrantLocal, force disable check_same_thread. Default: `False`
  62. Only use this if you can guarantee that you can resolve the thread safety outside QdrantClient.
  63. auth_token_provider: Callback function to get Bearer access token. If given, the function will be called before each request to get the token.
  64. check_compatibility: If `true` - check compatibility with the server version. Default: `true`
  65. **kwargs: Additional arguments passed directly into REST client initialization
  66. """
  67. def __init__(
  68. self,
  69. location: Optional[str] = None,
  70. url: Optional[str] = None,
  71. port: Optional[int] = 6333,
  72. grpc_port: int = 6334,
  73. prefer_grpc: bool = False,
  74. https: Optional[bool] = None,
  75. api_key: Optional[str] = None,
  76. prefix: Optional[str] = None,
  77. timeout: Optional[int] = None,
  78. host: Optional[str] = None,
  79. path: Optional[str] = None,
  80. force_disable_check_same_thread: bool = False,
  81. grpc_options: Optional[dict[str, Any]] = None,
  82. auth_token_provider: Optional[
  83. Union[Callable[[], str], Callable[[], Awaitable[str]]]
  84. ] = None,
  85. cloud_inference: bool = False,
  86. local_inference_batch_size: Optional[int] = None,
  87. check_compatibility: bool = True,
  88. **kwargs: Any,
  89. ):
  90. # Saving the init options to facilitate building AsyncQdrantClient from QdrantClient and vice versa.
  91. # Eg. AsyncQdrantClient(**sync_client.init_options) or QdrantClient(**async_client.init_options)
  92. self._init_options = {
  93. key: value
  94. for key, value in locals().items()
  95. if key not in ("self", "__class__", "kwargs")
  96. }
  97. self._init_options.update({k: v for k, v in kwargs.items()})
  98. self._inference_inspector = Inspector()
  99. super().__init__(
  100. parser=self._inference_inspector.parser, **kwargs
  101. ) # If we want to pass any kwargs to the parent class or ignore unexpected kwargs,
  102. # we will need to pop them from **kwargs. Otherwise, they might be passed to QdrantRemote as httpx kwargs.
  103. # Httpx has specific set of params, which it accepts and will raise an error if it receives any other params.
  104. self._client: QdrantBase
  105. if sum([param is not None for param in (location, url, host, path)]) > 1:
  106. raise ValueError(
  107. "Only one of <location>, <url>, <host> or <path> should be specified."
  108. )
  109. if location == ":memory:":
  110. self._client = QdrantLocal(
  111. location=location,
  112. force_disable_check_same_thread=force_disable_check_same_thread,
  113. )
  114. elif path is not None:
  115. self._client = QdrantLocal(
  116. location=path,
  117. force_disable_check_same_thread=force_disable_check_same_thread,
  118. )
  119. else:
  120. if location is not None and url is None:
  121. url = location
  122. self._client = QdrantRemote(
  123. url=url,
  124. port=port,
  125. grpc_port=grpc_port,
  126. prefer_grpc=prefer_grpc,
  127. https=https,
  128. api_key=api_key,
  129. prefix=prefix,
  130. timeout=timeout,
  131. host=host,
  132. grpc_options=grpc_options,
  133. auth_token_provider=auth_token_provider,
  134. check_compatibility=check_compatibility,
  135. **kwargs,
  136. )
  137. if isinstance(self._client, QdrantLocal) and cloud_inference:
  138. raise ValueError(
  139. "Cloud inference is not supported for local Qdrant, consider using FastEmbed or switch to Qdrant Cloud"
  140. )
  141. self.cloud_inference = cloud_inference
  142. self.local_inference_batch_size = local_inference_batch_size
  143. def __del__(self) -> None:
  144. self.close()
  145. def close(self, grpc_grace: Optional[float] = None, **kwargs: Any) -> None:
  146. """Closes the connection to Qdrant
  147. Args:
  148. grpc_grace: Grace period for gRPC connection close. Default: None
  149. """
  150. if hasattr(self, "_client"):
  151. self._client.close(grpc_grace=grpc_grace, **kwargs)
  152. @property
  153. def grpc_collections(self) -> grpc.CollectionsStub:
  154. """gRPC client for collections methods
  155. Returns:
  156. An instance of raw gRPC client, generated from Protobuf
  157. """
  158. if isinstance(self._client, QdrantRemote):
  159. return self._client.grpc_collections
  160. raise NotImplementedError(f"gRPC client is not supported for {type(self._client)}")
  161. @property
  162. def grpc_points(self) -> grpc.PointsStub:
  163. """gRPC client for points methods
  164. Returns:
  165. An instance of raw gRPC client, generated from Protobuf
  166. """
  167. if isinstance(self._client, QdrantRemote):
  168. return self._client.grpc_points
  169. raise NotImplementedError(f"gRPC client is not supported for {type(self._client)}")
  170. @property
  171. def rest(self) -> SyncApis[ApiClient]:
  172. """REST Client
  173. Returns:
  174. An instance of raw REST API client, generated from OpenAPI schema
  175. """
  176. warnings.warn(
  177. "The 'rest' property is deprecated and will be removed in a future version. Use `http` instead.",
  178. DeprecationWarning,
  179. stacklevel=2,
  180. )
  181. if isinstance(self._client, QdrantRemote):
  182. return self._client.rest
  183. raise NotImplementedError(f"REST client is not supported for {type(self._client)}")
  184. @property
  185. def http(self) -> SyncApis[ApiClient]:
  186. """REST Client
  187. Returns:
  188. An instance of raw REST API client, generated from OpenAPI schema
  189. """
  190. if isinstance(self._client, QdrantRemote):
  191. return self._client.http
  192. raise NotImplementedError(f"REST client is not supported for {type(self._client)}")
  193. @property
  194. def init_options(self) -> dict[str, Any]:
  195. """`__init__` Options
  196. Returns:
  197. A dictionary of options the client class was instantiated with
  198. """
  199. return self._init_options
  200. def search_batch(
  201. self,
  202. collection_name: str,
  203. requests: Sequence[types.SearchRequest],
  204. timeout: Optional[int] = None,
  205. consistency: Optional[types.ReadConsistency] = None,
  206. **kwargs: Any,
  207. ) -> list[list[types.ScoredPoint]]:
  208. """Perform multiple searches in a collection mitigating network overhead
  209. Args:
  210. collection_name: Name of the collection
  211. requests: List of search requests
  212. consistency:
  213. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  214. - int - number of replicas to query, values should present in all queried replicas
  215. - 'majority' - query all replicas, but return values present in the majority of replicas
  216. - 'quorum' - query the majority of replicas, return values present in all of them
  217. - 'all' - query all replicas, and return values present in all replicas
  218. timeout:
  219. Overrides global timeout for this search. Unit is seconds.
  220. Returns:
  221. List of search responses
  222. """
  223. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  224. warnings.warn(
  225. "`search_batch` method is deprecated and will be removed in the future."
  226. " Use `query_batch_points` instead.",
  227. DeprecationWarning,
  228. stacklevel=2,
  229. )
  230. return self._client.search_batch(
  231. collection_name=collection_name,
  232. requests=requests,
  233. consistency=consistency,
  234. timeout=timeout,
  235. **kwargs,
  236. )
  237. def search(
  238. self,
  239. collection_name: str,
  240. query_vector: Union[
  241. Sequence[float],
  242. tuple[str, list[float]],
  243. types.NamedVector,
  244. types.NamedSparseVector,
  245. types.NumpyArray,
  246. ],
  247. query_filter: Optional[types.Filter] = None,
  248. search_params: Optional[types.SearchParams] = None,
  249. limit: int = 10,
  250. offset: Optional[int] = None,
  251. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  252. with_vectors: Union[bool, Sequence[str]] = False,
  253. score_threshold: Optional[float] = None,
  254. append_payload: bool = True,
  255. consistency: Optional[types.ReadConsistency] = None,
  256. shard_key_selector: Optional[types.ShardKeySelector] = None,
  257. timeout: Optional[int] = None,
  258. **kwargs: Any,
  259. ) -> list[types.ScoredPoint]:
  260. """Search for closest vectors in collection taking into account filtering conditions
  261. Args:
  262. collection_name: Collection to search in
  263. query_vector:
  264. Search for vectors closest to this.
  265. Can be either a vector itself, or a named vector, or a named sparse vector, or a tuple of vector name and vector itself
  266. query_filter:
  267. - Exclude vectors which doesn't fit given conditions.
  268. - If `None` - search among all vectors
  269. search_params: Additional search params
  270. limit: How many results return
  271. offset:
  272. Offset of the first result to return.
  273. May be used to paginate results.
  274. Note: large offset values may cause performance issues.
  275. with_payload:
  276. - Specify which stored payload should be attached to the result.
  277. - If `True` - attach all payload
  278. - If `False` - do not attach any payload
  279. - If List of string - include only specified fields
  280. - If `PayloadSelector` - use explicit rules
  281. with_vectors:
  282. - If `True` - Attach stored vector to the search result.
  283. - If `False` - Do not attach vector.
  284. - If List of string - include only specified fields
  285. - Default: `False`
  286. score_threshold:
  287. Define a minimal score threshold for the result.
  288. If defined, less similar results will not be returned.
  289. Score of the returned result might be higher or smaller than the threshold depending
  290. on the Distance function used.
  291. E.g. for cosine similarity only higher scores will be returned.
  292. append_payload: Same as `with_payload`. Deprecated.
  293. consistency:
  294. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  295. - int - number of replicas to query, values should present in all queried replicas
  296. - 'majority' - query all replicas, but return values present in the majority of replicas
  297. - 'quorum' - query the majority of replicas, return values present in all of them
  298. - 'all' - query all replicas, and return values present in all replicas
  299. shard_key_selector:
  300. This parameter allows to specify which shards should be queried.
  301. If `None` - query all shards. Only works for collections with `custom` sharding method.
  302. timeout:
  303. Overrides global timeout for this search. Unit is seconds.
  304. Examples:
  305. `Search with filter`::
  306. qdrant.search(
  307. collection_name="test_collection",
  308. query_vector=[1.0, 0.1, 0.2, 0.7],
  309. query_filter=Filter(
  310. must=[
  311. FieldCondition(
  312. key='color',
  313. range=Match(
  314. value="red"
  315. )
  316. )
  317. ]
  318. )
  319. )
  320. Returns:
  321. List of found close points with similarity scores.
  322. """
  323. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  324. warnings.warn(
  325. "`search` method is deprecated and will be removed in the future."
  326. " Use `query_points` instead.",
  327. DeprecationWarning,
  328. stacklevel=2,
  329. )
  330. return self._client.search(
  331. collection_name=collection_name,
  332. query_vector=query_vector,
  333. query_filter=query_filter,
  334. search_params=search_params,
  335. limit=limit,
  336. offset=offset,
  337. with_payload=with_payload,
  338. with_vectors=with_vectors,
  339. score_threshold=score_threshold,
  340. append_payload=append_payload,
  341. consistency=consistency,
  342. shard_key_selector=shard_key_selector,
  343. timeout=timeout,
  344. **kwargs,
  345. )
  346. def query_batch_points(
  347. self,
  348. collection_name: str,
  349. requests: Sequence[types.QueryRequest],
  350. consistency: Optional[types.ReadConsistency] = None,
  351. timeout: Optional[int] = None,
  352. **kwargs: Any,
  353. ) -> list[types.QueryResponse]:
  354. """Perform any search, recommend, discovery, context search operations in batch, and mitigate network overhead
  355. Args:
  356. collection_name: Name of the collection
  357. requests: List of query requests
  358. consistency:
  359. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  360. - int - number of replicas to query, values should present in all queried replicas
  361. - 'majority' - query all replicas, but return values present in the majority of replicas
  362. - 'quorum' - query the majority of replicas, return values present in all of them
  363. - 'all' - query all replicas, and return values present in all replicas
  364. timeout:
  365. Overrides global timeout for this search. Unit is seconds.
  366. Returns:
  367. List of query responses
  368. """
  369. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  370. requests = self._resolve_query_batch_request(requests)
  371. if not self.cloud_inference and self._inference_inspector.inspect(requests):
  372. requests = list(
  373. self._embed_models(
  374. requests, is_query=True, batch_size=self.local_inference_batch_size
  375. )
  376. )
  377. return self._client.query_batch_points(
  378. collection_name=collection_name,
  379. requests=requests,
  380. consistency=consistency,
  381. timeout=timeout,
  382. **kwargs,
  383. )
  384. def query_points(
  385. self,
  386. collection_name: str,
  387. query: Union[
  388. types.PointId,
  389. list[float],
  390. list[list[float]],
  391. types.SparseVector,
  392. types.Query,
  393. types.NumpyArray,
  394. types.Document,
  395. types.Image,
  396. types.InferenceObject,
  397. None,
  398. ] = None,
  399. using: Optional[str] = None,
  400. prefetch: Union[types.Prefetch, list[types.Prefetch], None] = None,
  401. query_filter: Optional[types.Filter] = None,
  402. search_params: Optional[types.SearchParams] = None,
  403. limit: int = 10,
  404. offset: Optional[int] = None,
  405. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  406. with_vectors: Union[bool, Sequence[str]] = False,
  407. score_threshold: Optional[float] = None,
  408. lookup_from: Optional[types.LookupLocation] = None,
  409. consistency: Optional[types.ReadConsistency] = None,
  410. shard_key_selector: Optional[types.ShardKeySelector] = None,
  411. timeout: Optional[int] = None,
  412. **kwargs: Any,
  413. ) -> types.QueryResponse:
  414. """Universal endpoint to run any available operation, such as search, recommendation, discovery, context search.
  415. Args:
  416. collection_name: Collection to search in
  417. query:
  418. Query for the chosen search type operation.
  419. - If `str` - use string as UUID of the existing point as a search query.
  420. - If `int` - use integer as ID of the existing point as a search query.
  421. - If `list[float]` - use as a dense vector for nearest search.
  422. - If `list[list[float]]` - use as a multi-vector for nearest search.
  423. - If `SparseVector` - use as a sparse vector for nearest search.
  424. - If `Query` - use as a query for specific search type.
  425. - If `NumpyArray` - use as a dense vector for nearest search.
  426. - If `Document` - infer vector from the document text and use it for nearest search (requires `fastembed` package installed).
  427. - If `None` - return first `limit` points from the collection.
  428. prefetch: prefetch queries to make a selection of the data to be used with the main query
  429. query_filter:
  430. - Exclude vectors which doesn't fit given conditions.
  431. - If `None` - search among all vectors
  432. search_params: Additional search params
  433. limit: How many results return
  434. offset:
  435. Offset of the first result to return.
  436. May be used to paginate results.
  437. Note: large offset values may cause performance issues.
  438. with_payload:
  439. - Specify which stored payload should be attached to the result.
  440. - If `True` - attach all payload
  441. - If `False` - do not attach any payload
  442. - If List of string - include only specified fields
  443. - If `PayloadSelector` - use explicit rules
  444. with_vectors:
  445. - If `True` - Attach stored vector to the search result.
  446. - If `False` - Do not attach vector.
  447. - If List of string - include only specified fields
  448. - Default: `False`
  449. score_threshold:
  450. Define a minimal score threshold for the result.
  451. If defined, less similar results will not be returned.
  452. Score of the returned result might be higher or smaller than the threshold depending
  453. on the Distance function used.
  454. E.g. for cosine similarity only higher scores will be returned.
  455. using:
  456. Name of the vectors to use for query.
  457. If `None` - use default vectors or provided in named vector structures.
  458. lookup_from:
  459. Defines a location (collection and vector field name), used to lookup vectors for recommendations,
  460. discovery and context queries.
  461. If `None` - current collection will be used.
  462. consistency:
  463. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  464. - int - number of replicas to query, values should present in all queried replicas
  465. - 'majority' - query all replicas, but return values present in the majority of replicas
  466. - 'quorum' - query the majority of replicas, return values present in all of them
  467. - 'all' - query all replicas, and return values present in all replicas
  468. shard_key_selector:
  469. This parameter allows to specify which shards should be queried.
  470. If `None` - query all shards. Only works for collections with `custom` sharding method.
  471. timeout:
  472. Overrides global timeout for this search. Unit is seconds.
  473. Examples:
  474. `Search for closest points with a filter`::
  475. qdrant.query(
  476. collection_name="test_collection",
  477. query=[1.0, 0.1, 0.2, 0.7],
  478. query_filter=Filter(
  479. must=[
  480. FieldCondition(
  481. key='color',
  482. range=Match(
  483. value="red"
  484. )
  485. )
  486. ]
  487. )
  488. )
  489. Returns:
  490. QueryResponse structure containing list of found close points with similarity scores.
  491. """
  492. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  493. # If the query contains unprocessed documents, we need to embed them and
  494. # replace the original query with the embedded vectors.
  495. query = self._resolve_query(query)
  496. if not self.cloud_inference:
  497. if self._inference_inspector.inspect(query) or self._inference_inspector.inspect(
  498. prefetch
  499. ):
  500. query = (
  501. next(
  502. iter(
  503. self._embed_models(
  504. query, is_query=True, batch_size=self.local_inference_batch_size
  505. )
  506. )
  507. )
  508. if query is not None
  509. else None
  510. )
  511. if isinstance(prefetch, list):
  512. prefetch = list(
  513. self._embed_models(
  514. prefetch, is_query=True, batch_size=self.local_inference_batch_size
  515. )
  516. )
  517. else:
  518. prefetch = (
  519. next(
  520. iter(
  521. self._embed_models(
  522. prefetch,
  523. is_query=True,
  524. batch_size=self.local_inference_batch_size,
  525. )
  526. )
  527. )
  528. if prefetch is not None
  529. else None
  530. )
  531. return self._client.query_points(
  532. collection_name=collection_name,
  533. query=query,
  534. prefetch=prefetch,
  535. query_filter=query_filter,
  536. search_params=search_params,
  537. limit=limit,
  538. offset=offset,
  539. with_payload=with_payload,
  540. with_vectors=with_vectors,
  541. score_threshold=score_threshold,
  542. using=using,
  543. lookup_from=lookup_from,
  544. consistency=consistency,
  545. shard_key_selector=shard_key_selector,
  546. timeout=timeout,
  547. **kwargs,
  548. )
  549. def query_points_groups(
  550. self,
  551. collection_name: str,
  552. group_by: str,
  553. query: Union[
  554. types.PointId,
  555. list[float],
  556. list[list[float]],
  557. types.SparseVector,
  558. types.Query,
  559. types.NumpyArray,
  560. types.Document,
  561. types.Image,
  562. types.InferenceObject,
  563. None,
  564. ] = None,
  565. using: Optional[str] = None,
  566. prefetch: Union[types.Prefetch, list[types.Prefetch], None] = None,
  567. query_filter: Optional[types.Filter] = None,
  568. search_params: Optional[types.SearchParams] = None,
  569. limit: int = 10,
  570. group_size: int = 3,
  571. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  572. with_vectors: Union[bool, Sequence[str]] = False,
  573. score_threshold: Optional[float] = None,
  574. with_lookup: Optional[types.WithLookupInterface] = None,
  575. lookup_from: Optional[types.LookupLocation] = None,
  576. consistency: Optional[types.ReadConsistency] = None,
  577. shard_key_selector: Optional[types.ShardKeySelector] = None,
  578. timeout: Optional[int] = None,
  579. **kwargs: Any,
  580. ) -> types.GroupsResult:
  581. """Universal endpoint to group on any available operation, such as search, recommendation, discovery, context search.
  582. Args:
  583. collection_name: Collection to search in
  584. query:
  585. Query for the chosen search type operation.
  586. - If `str` - use string as UUID of the existing point as a search query.
  587. - If `int` - use integer as ID of the existing point as a search query.
  588. - If `list[float]` - use as a dense vector for nearest search.
  589. - If `list[list[float]]` - use as a multi-vector for nearest search.
  590. - If `SparseVector` - use as a sparse vector for nearest search.
  591. - If `Query` - use as a query for specific search type.
  592. - If `NumpyArray` - use as a dense vector for nearest search.
  593. - If `Document` - infer vector from the document text and use it for nearest search (requires `fastembed` package installed).
  594. - If `None` - return first `limit` points from the collection.
  595. prefetch: prefetch queries to make a selection of the data to be used with the main query
  596. query_filter:
  597. - Exclude vectors which doesn't fit given conditions.
  598. - If `None` - search among all vectors
  599. search_params: Additional search params
  600. limit: How many results return
  601. group_size: How many results return for each group
  602. group_by: Name of the payload field to group by. Field must be of type "keyword" or "integer".
  603. Nested fields are specified using dot notation, e.g. "nested_field.subfield".
  604. with_payload:
  605. - Specify which stored payload should be attached to the result.
  606. - If `True` - attach all payload
  607. - If `False` - do not attach any payload
  608. - If List of string - include only specified fields
  609. - If `PayloadSelector` - use explicit rules
  610. with_vectors:
  611. - If `True` - Attach stored vector to the search result.
  612. - If `False` - Do not attach vector.
  613. - If List of string - include only specified fields
  614. - Default: `False`
  615. score_threshold:
  616. Define a minimal score threshold for the result.
  617. If defined, less similar results will not be returned.
  618. Score of the returned result might be higher or smaller than the threshold depending
  619. on the Distance function used.
  620. E.g. for cosine similarity only higher scores will be returned.
  621. using:
  622. Name of the vectors to use for query.
  623. If `None` - use default vectors or provided in named vector structures.
  624. with_lookup:
  625. Look for points in another collection using the group ids.
  626. If specified, each group will contain a record from the specified collection
  627. with the same id as the group id. In addition, the parameter allows to specify
  628. which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
  629. lookup_from:
  630. Defines a location (collection and vector field name), used to lookup vectors being referenced in the query as IDs.
  631. If `None` - current collection will be used.
  632. consistency:
  633. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  634. - int - number of replicas to query, values should present in all queried replicas
  635. - 'majority' - query all replicas, but return values present in the majority of replicas
  636. - 'quorum' - query the majority of replicas, return values present in all of them
  637. - 'all' - query all replicas, and return values present in all replicas
  638. shard_key_selector:
  639. This parameter allows to specify which shards should be queried.
  640. If `None` - query all shards. Only works for collections with `custom` sharding method.
  641. timeout:
  642. Overrides global timeout for this search. Unit is seconds.
  643. Examples:
  644. `Search for closest points and group results`::
  645. qdrant.query_points_groups(
  646. collection_name="test_collection",
  647. query=[1.0, 0.1, 0.2, 0.7],
  648. group_by="color",
  649. group_size=3,
  650. )
  651. Returns:
  652. List of groups with not more than `group_size` hits in each group.
  653. Each group also contains an id of the group, which is the value of the payload field.
  654. """
  655. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  656. # If the query contains unprocessed documents, we need to embed them and
  657. # replace the original query with the embedded vectors.
  658. query = self._resolve_query(query)
  659. if not self.cloud_inference:
  660. if self._inference_inspector.inspect(query) or self._inference_inspector.inspect(
  661. prefetch
  662. ):
  663. query = (
  664. next(
  665. iter(
  666. self._embed_models(
  667. query, is_query=True, batch_size=self.local_inference_batch_size
  668. )
  669. )
  670. )
  671. if query is not None
  672. else None
  673. )
  674. if isinstance(prefetch, list):
  675. prefetch = list(
  676. self._embed_models(
  677. prefetch, is_query=True, batch_size=self.local_inference_batch_size
  678. )
  679. )
  680. elif prefetch is not None:
  681. prefetch = next(
  682. iter(
  683. self._embed_models(
  684. prefetch, is_query=True, batch_size=self.local_inference_batch_size
  685. )
  686. )
  687. )
  688. return self._client.query_points_groups(
  689. collection_name=collection_name,
  690. query=query,
  691. prefetch=prefetch,
  692. query_filter=query_filter,
  693. search_params=search_params,
  694. group_by=group_by,
  695. limit=limit,
  696. group_size=group_size,
  697. with_payload=with_payload,
  698. with_vectors=with_vectors,
  699. score_threshold=score_threshold,
  700. using=using,
  701. with_lookup=with_lookup,
  702. consistency=consistency,
  703. shard_key_selector=shard_key_selector,
  704. timeout=timeout,
  705. **kwargs,
  706. )
  707. def search_groups(
  708. self,
  709. collection_name: str,
  710. query_vector: Union[
  711. Sequence[float],
  712. tuple[str, list[float]],
  713. types.NamedVector,
  714. types.NamedSparseVector,
  715. types.NumpyArray,
  716. ],
  717. group_by: str,
  718. query_filter: Optional[types.Filter] = None,
  719. search_params: Optional[types.SearchParams] = None,
  720. limit: int = 10,
  721. group_size: int = 1,
  722. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  723. with_vectors: Union[bool, Sequence[str]] = False,
  724. score_threshold: Optional[float] = None,
  725. with_lookup: Optional[types.WithLookupInterface] = None,
  726. consistency: Optional[types.ReadConsistency] = None,
  727. shard_key_selector: Optional[types.ShardKeySelector] = None,
  728. timeout: Optional[int] = None,
  729. **kwargs: Any,
  730. ) -> types.GroupsResult:
  731. """Search for closest vectors grouped by payload field.
  732. Searches best matches for query vector grouped by the value of payload field.
  733. Useful to obtain most relevant results for each category, deduplicate results,
  734. finding the best representation vector for the same entity.
  735. Args:
  736. collection_name: Collection to search in
  737. query_vector:
  738. Search for vectors closest to this.
  739. Can be either a vector itself, or a named vector, or a named sparse vector, or a tuple of vector name and vector itself
  740. group_by: Name of the payload field to group by.
  741. Field must be of type "keyword" or "integer".
  742. Nested fields are specified using dot notation, e.g. "nested_field.subfield".
  743. query_filter:
  744. - Exclude vectors which doesn't fit given conditions.
  745. - If `None` - search among all vectors
  746. search_params: Additional search params
  747. limit: How many groups return
  748. group_size: How many results return for each group
  749. with_payload:
  750. - Specify which stored payload should be attached to the result.
  751. - If `True` - attach all payload
  752. - If `False` - do not attach any payload
  753. - If List of string - include only specified fields
  754. - If `PayloadSelector` - use explicit rules
  755. with_vectors:
  756. - If `True` - Attach stored vector to the search result.
  757. - If `False` - Do not attach vector.
  758. - If List of string - include only specified fields
  759. - Default: `False`
  760. score_threshold: Minimal score threshold for the result.
  761. If defined, less similar results will not be returned.
  762. Score of the returned result might be higher or smaller than the threshold depending
  763. on the Distance function used.
  764. E.g. for cosine similarity only higher scores will be returned.
  765. with_lookup:
  766. Look for points in another collection using the group ids.
  767. If specified, each group will contain a record from the specified collection
  768. with the same id as the group id. In addition, the parameter allows to specify
  769. which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
  770. consistency:
  771. Read consistency of the search. Defines how many replicas should be queried before returning the result.
  772. Values:
  773. - int - number of replicas to query, values should present in all queried replicas
  774. - 'majority' - query all replicas, but return values present in the majority of replicas
  775. - 'quorum' - query the majority of replicas, return values present in all of them
  776. - 'all' - query all replicas, and return values present in all replicas
  777. shard_key_selector:
  778. This parameter allows to specify which shards should be queried.
  779. If `None` - query all shards. Only works for collections with `custom` sharding method.
  780. timeout:
  781. Overrides global timeout for this search. Unit is seconds.
  782. Returns:
  783. List of groups with not more than `group_size` hits in each group.
  784. Each group also contains an id of the group, which is the value of the payload field.
  785. """
  786. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  787. warnings.warn(
  788. "`search_groups` method is deprecated and will be removed in the future."
  789. " Use `query_points_groups` instead.",
  790. DeprecationWarning,
  791. stacklevel=2,
  792. )
  793. return self._client.search_groups(
  794. collection_name=collection_name,
  795. query_vector=query_vector,
  796. group_by=group_by,
  797. query_filter=query_filter,
  798. search_params=search_params,
  799. limit=limit,
  800. group_size=group_size,
  801. with_payload=with_payload,
  802. with_vectors=with_vectors,
  803. score_threshold=score_threshold,
  804. with_lookup=with_lookup,
  805. consistency=consistency,
  806. shard_key_selector=shard_key_selector,
  807. timeout=timeout,
  808. **kwargs,
  809. )
  810. def recommend_batch(
  811. self,
  812. collection_name: str,
  813. requests: Sequence[types.RecommendRequest],
  814. consistency: Optional[types.ReadConsistency] = None,
  815. timeout: Optional[int] = None,
  816. **kwargs: Any,
  817. ) -> list[list[types.ScoredPoint]]:
  818. """Perform multiple recommend requests in batch mode
  819. Args:
  820. collection_name: Name of the collection
  821. requests: List of recommend requests
  822. consistency:
  823. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  824. - int - number of replicas to query, values should present in all queried replicas
  825. - 'majority' - query all replicas, but return values present in the majority of replicas
  826. - 'quorum' - query the majority of replicas, return values present in all of them
  827. - 'all' - query all replicas, and return values present in all replicas
  828. timeout:
  829. Overrides global timeout for this search. Unit is seconds.
  830. Returns:
  831. List of recommend responses
  832. """
  833. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  834. warnings.warn(
  835. "`recommend_batch` method is deprecated and will be removed in the future."
  836. " Use `query_batch_points` instead.",
  837. DeprecationWarning,
  838. stacklevel=2,
  839. )
  840. return self._client.recommend_batch(
  841. collection_name=collection_name,
  842. requests=requests,
  843. consistency=consistency,
  844. timeout=timeout,
  845. **kwargs,
  846. )
  847. def recommend(
  848. self,
  849. collection_name: str,
  850. positive: Optional[Sequence[types.RecommendExample]] = None,
  851. negative: Optional[Sequence[types.RecommendExample]] = None,
  852. query_filter: Optional[types.Filter] = None,
  853. search_params: Optional[types.SearchParams] = None,
  854. limit: int = 10,
  855. offset: int = 0,
  856. with_payload: Union[bool, list[str], types.PayloadSelector] = True,
  857. with_vectors: Union[bool, list[str]] = False,
  858. score_threshold: Optional[float] = None,
  859. using: Optional[str] = None,
  860. lookup_from: Optional[types.LookupLocation] = None,
  861. strategy: Optional[types.RecommendStrategy] = None,
  862. consistency: Optional[types.ReadConsistency] = None,
  863. shard_key_selector: Optional[types.ShardKeySelector] = None,
  864. timeout: Optional[int] = None,
  865. **kwargs: Any,
  866. ) -> list[types.ScoredPoint]:
  867. """Recommend points: search for similar points based on already stored in Qdrant examples.
  868. Provide IDs of the stored points, and Qdrant will perform search based on already existing vectors.
  869. This functionality is especially useful for recommendation over existing collection of points.
  870. Args:
  871. collection_name: Collection to search in
  872. positive:
  873. List of stored point IDs or vectors, which should be used as reference for similarity search.
  874. If there is only one example - this request is equivalent to the regular search with vector of that
  875. point.
  876. If there are more than one example, Qdrant will attempt to search for similar to all of them.
  877. Recommendation for multiple vectors is experimental.
  878. Its behaviour may change depending on selected strategy.
  879. negative:
  880. List of stored point IDs or vectors, which should be dissimilar to the search result.
  881. Negative examples is an experimental functionality.
  882. Its behaviour may change depending on selected strategy.
  883. query_filter:
  884. - Exclude vectors which doesn't fit given conditions.
  885. - If `None` - search among all vectors
  886. search_params: Additional search params
  887. limit: How many results return
  888. offset:
  889. Offset of the first result to return.
  890. May be used to paginate results.
  891. Note: large offset values may cause performance issues.
  892. with_payload:
  893. - Specify which stored payload should be attached to the result.
  894. - If `True` - attach all payload
  895. - If `False` - do not attach any payload
  896. - If List of string - include only specified fields
  897. - If `PayloadSelector` - use explicit rules
  898. with_vectors:
  899. - If `True` - Attach stored vector to the search result.
  900. - If `False` - Do not attach vector.
  901. - If List of string - include only specified fields
  902. - Default: `False`
  903. score_threshold:
  904. Define a minimal score threshold for the result.
  905. If defined, less similar results will not be returned.
  906. Score of the returned result might be higher or smaller than the threshold depending
  907. on the Distance function used.
  908. E.g. for cosine similarity only higher scores will be returned.
  909. using:
  910. Name of the vectors to use for recommendations.
  911. If `None` - use default vectors.
  912. lookup_from:
  913. Defines a location (collection and vector field name), used to lookup vectors for recommendations.
  914. If `None` - current collection will be used.
  915. consistency:
  916. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  917. - int - number of replicas to query, values should present in all queried replicas
  918. - 'majority' - query all replicas, but return values present in the majority of replicas
  919. - 'quorum' - query the majority of replicas, return values present in all of them
  920. - 'all' - query all replicas, and return values present in all replicas
  921. shard_key_selector:
  922. This parameter allows to specify which shards should be queried.
  923. If `None` - query all shards. Only works for collections with `custom` sharding method.
  924. strategy:
  925. Strategy to use for recommendation.
  926. Strategy defines how to combine multiple examples into a recommendation query.
  927. Possible values:
  928. - 'average_vector' - calculates average vector of all examples and uses it for search
  929. - 'best_score' - finds the result which is closer to positive examples and further from negative
  930. timeout:
  931. Overrides global timeout for this search. Unit is seconds.
  932. Returns:
  933. List of recommended points with similarity scores.
  934. """
  935. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  936. warnings.warn(
  937. "`recommend` method is deprecated and will be removed in the future."
  938. " Use `query_points` instead.",
  939. DeprecationWarning,
  940. stacklevel=2,
  941. )
  942. return self._client.recommend(
  943. collection_name=collection_name,
  944. positive=positive,
  945. negative=negative,
  946. query_filter=query_filter,
  947. search_params=search_params,
  948. limit=limit,
  949. offset=offset,
  950. with_payload=with_payload,
  951. with_vectors=with_vectors,
  952. score_threshold=score_threshold,
  953. using=using,
  954. lookup_from=lookup_from,
  955. consistency=consistency,
  956. shard_key_selector=shard_key_selector,
  957. strategy=strategy,
  958. timeout=timeout,
  959. **kwargs,
  960. )
  961. def search_matrix_pairs(
  962. self,
  963. collection_name: str,
  964. query_filter: Optional[types.Filter] = None,
  965. limit: int = 3,
  966. sample: int = 10,
  967. using: Optional[str] = None,
  968. consistency: Optional[types.ReadConsistency] = None,
  969. timeout: Optional[int] = None,
  970. shard_key_selector: Optional[types.ShardKeySelector] = None,
  971. **kwargs: Any,
  972. ) -> types.SearchMatrixPairsResponse:
  973. """
  974. Compute distance matrix for sampled points with a pair-based output format.
  975. Args:
  976. collection_name: Name of the collection.
  977. query_filter: Filter to apply.
  978. limit: How many neighbors per sample to find.
  979. sample: How many points to select and search within.
  980. using: Name of the vectors to use for search. If `None`, use default vectors.
  981. consistency: Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  982. - int: Number of replicas to query, values should be present in all queried replicas.
  983. - 'majority': Query all replicas, but return values present in the majority of replicas.
  984. - 'quorum': Query the majority of replicas, return values present in all of them.
  985. - 'all': Query all replicas, and return values present in all replicas.
  986. timeout: Overrides global timeout for this search. Unit is seconds.
  987. shard_key_selector: This parameter allows specifying which shards should be queried.
  988. If `None`, query all shards. Only works for collections with the `custom` sharding method.
  989. Returns:
  990. Distance matrix using a pair-based encoding.
  991. """
  992. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  993. return self._client.search_matrix_pairs(
  994. collection_name=collection_name,
  995. query_filter=query_filter,
  996. limit=limit,
  997. sample=sample,
  998. using=using,
  999. consistency=consistency,
  1000. timeout=timeout,
  1001. shard_key_selector=shard_key_selector,
  1002. **kwargs,
  1003. )
  1004. def search_matrix_offsets(
  1005. self,
  1006. collection_name: str,
  1007. query_filter: Optional[types.Filter] = None,
  1008. limit: int = 3,
  1009. sample: int = 10,
  1010. using: Optional[str] = None,
  1011. consistency: Optional[types.ReadConsistency] = None,
  1012. timeout: Optional[int] = None,
  1013. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1014. **kwargs: Any,
  1015. ) -> types.SearchMatrixOffsetsResponse:
  1016. """
  1017. Compute distance matrix for sampled points with an offset-based output format.
  1018. Args:
  1019. collection_name: Name of the collection.
  1020. query_filter: Filter to apply.
  1021. limit: How many neighbors per sample to find.
  1022. sample: How many points to select and search within.
  1023. using: Name of the vectors to use for search. If `None`, use default vectors.
  1024. consistency: Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1025. - int: Number of replicas to query, values should present in all queried replicas.
  1026. - 'majority': Query all replicas, but return values present in the majority of replicas.
  1027. - 'quorum': Query the majority of replicas, return values present in all of them.
  1028. - 'all': Query all replicas and return values present in all replicas.
  1029. timeout: Overrides global timeout for this search. Unit is seconds.
  1030. shard_key_selector: This parameter allows specifying which shards should be queried.
  1031. If `None`, query all shards. Only works for collections with the `custom` sharding method.
  1032. Returns:
  1033. Distance matrix using an offset-based encoding.
  1034. """
  1035. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1036. return self._client.search_matrix_offsets(
  1037. collection_name=collection_name,
  1038. query_filter=query_filter,
  1039. limit=limit,
  1040. sample=sample,
  1041. using=using,
  1042. consistency=consistency,
  1043. timeout=timeout,
  1044. shard_key_selector=shard_key_selector,
  1045. **kwargs,
  1046. )
  1047. def recommend_groups(
  1048. self,
  1049. collection_name: str,
  1050. group_by: str,
  1051. positive: Optional[Sequence[types.RecommendExample]] = None,
  1052. negative: Optional[Sequence[types.RecommendExample]] = None,
  1053. query_filter: Optional[types.Filter] = None,
  1054. search_params: Optional[types.SearchParams] = None,
  1055. limit: int = 10,
  1056. group_size: int = 1,
  1057. score_threshold: Optional[float] = None,
  1058. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  1059. with_vectors: Union[bool, Sequence[str]] = False,
  1060. using: Optional[str] = None,
  1061. lookup_from: Optional[types.LookupLocation] = None,
  1062. with_lookup: Optional[types.WithLookupInterface] = None,
  1063. strategy: Optional[types.RecommendStrategy] = None,
  1064. consistency: Optional[types.ReadConsistency] = None,
  1065. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1066. timeout: Optional[int] = None,
  1067. **kwargs: Any,
  1068. ) -> types.GroupsResult:
  1069. """Recommend point groups: search for similar points based on already stored in Qdrant examples
  1070. and groups by payload field.
  1071. Recommend best matches for given stored examples grouped by the value of payload field.
  1072. Useful to obtain most relevant results for each category, deduplicate results,
  1073. finding the best representation vector for the same entity.
  1074. Args:
  1075. collection_name: Collection to search in
  1076. positive:
  1077. List of stored point IDs or vectors, which should be used as reference for similarity search.
  1078. If there is only one example - this request is equivalent to the regular search with vector of that
  1079. point.
  1080. If there are more than one example, Qdrant will attempt to search for similar to all of them.
  1081. Recommendation for multiple vectors is experimental.
  1082. Its behaviour may change depending on selected strategy.
  1083. negative:
  1084. List of stored point IDs or vectors, which should be dissimilar to the search result.
  1085. Negative examples is an experimental functionality.
  1086. Its behaviour may change depending on selected strategy.
  1087. group_by: Name of the payload field to group by.
  1088. Field must be of type "keyword" or "integer".
  1089. Nested fields are specified using dot notation, e.g. "nested_field.subfield".
  1090. query_filter:
  1091. - Exclude vectors which doesn't fit given conditions.
  1092. - If `None` - search among all vectors
  1093. search_params: Additional search params
  1094. limit: How many groups return
  1095. group_size: How many results return for each group
  1096. with_payload:
  1097. - Specify which stored payload should be attached to the result.
  1098. - If `True` - attach all payload
  1099. - If `False` - do not attach any payload
  1100. - If List of string - include only specified fields
  1101. - If `PayloadSelector` - use explicit rules
  1102. with_vectors:
  1103. - If `True` - Attach stored vector to the search result.
  1104. - If `False` - Do not attach vector.
  1105. - If List of string - include only specified fields
  1106. - Default: `False`
  1107. score_threshold:
  1108. Define a minimal score threshold for the result.
  1109. If defined, less similar results will not be returned.
  1110. Score of the returned result might be higher or smaller than the threshold depending
  1111. on the Distance function used.
  1112. E.g. for cosine similarity only higher scores will be returned.
  1113. using:
  1114. Name of the vectors to use for recommendations.
  1115. If `None` - use default vectors.
  1116. lookup_from:
  1117. Defines a location (collection and vector field name), used to lookup vectors for recommendations.
  1118. If `None` - current collection will be used.
  1119. with_lookup:
  1120. Look for points in another collection using the group ids.
  1121. If specified, each group will contain a record from the specified collection
  1122. with the same id as the group id. In addition, the parameter allows to specify
  1123. which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
  1124. consistency:
  1125. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1126. - int - number of replicas to query, values should present in all queried replicas
  1127. - 'majority' - query all replicas, but return values present in the majority of replicas
  1128. - 'quorum' - query the majority of replicas, return values present in all of them
  1129. - 'all' - query all replicas, and return values present in all replicas
  1130. shard_key_selector:
  1131. This parameter allows to specify which shards should be queried.
  1132. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1133. strategy:
  1134. Strategy to use for recommendation.
  1135. Strategy defines how to combine multiple examples into a recommendation query.
  1136. Possible values:
  1137. - 'average_vector' - calculates average vector of all examples and uses it for search
  1138. - 'best_score' - finds the result which is closer to positive examples and further from negative
  1139. timeout:
  1140. Overrides global timeout for this search. Unit is seconds.
  1141. Returns:
  1142. List of groups with not more than `group_size` hits in each group.
  1143. Each group also contains an id of the group, which is the value of the payload field.
  1144. """
  1145. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1146. warnings.warn(
  1147. "`recommend_groups` method is deprecated and will be removed in the future."
  1148. " Use `query_points_groups` instead.",
  1149. DeprecationWarning,
  1150. stacklevel=2,
  1151. )
  1152. return self._client.recommend_groups(
  1153. collection_name=collection_name,
  1154. group_by=group_by,
  1155. positive=positive,
  1156. negative=negative,
  1157. query_filter=query_filter,
  1158. search_params=search_params,
  1159. limit=limit,
  1160. group_size=group_size,
  1161. score_threshold=score_threshold,
  1162. with_payload=with_payload,
  1163. with_vectors=with_vectors,
  1164. using=using,
  1165. lookup_from=lookup_from,
  1166. with_lookup=with_lookup,
  1167. strategy=strategy,
  1168. consistency=consistency,
  1169. shard_key_selector=shard_key_selector,
  1170. timeout=timeout,
  1171. **kwargs,
  1172. )
  1173. def discover(
  1174. self,
  1175. collection_name: str,
  1176. target: Optional[types.TargetVector] = None,
  1177. context: Optional[Sequence[types.ContextExamplePair]] = None,
  1178. query_filter: Optional[types.Filter] = None,
  1179. search_params: Optional[types.SearchParams] = None,
  1180. limit: int = 10,
  1181. offset: int = 0,
  1182. with_payload: Union[bool, list[str], types.PayloadSelector] = True,
  1183. with_vectors: Union[bool, list[str]] = False,
  1184. using: Optional[str] = None,
  1185. lookup_from: Optional[types.LookupLocation] = None,
  1186. consistency: Optional[types.ReadConsistency] = None,
  1187. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1188. timeout: Optional[int] = None,
  1189. **kwargs: Any,
  1190. ) -> list[types.ScoredPoint]:
  1191. """
  1192. Use context and a target to find the most similar points, constrained by the context.
  1193. Args:
  1194. collection_name: Collection to discover in
  1195. target:
  1196. Look for vectors closest to this.
  1197. When using the target (with or without context), the integer part of the score represents the rank with respect to the context, while the decimal part of the score relates to the distance to the target.
  1198. context:
  1199. Pairs of { positive, negative } examples to constrain the search.
  1200. When using only the context (without a target), a special search - called context search - is performed where pairs of points are used to generate a loss that guides the search towards the zone where most positive examples overlap. This means that the score minimizes the scenario of finding a point closer to a negative than to a positive part of a pair.
  1201. Since the score of a context relates to loss, the maximum score a point can get is 0.0, and it becomes normal that many points can have a score of 0.0.
  1202. For discovery search (when including a target), the context part of the score for each pair is calculated +1 if the point is closer to a positive than to a negative part of a pair, and -1 otherwise.
  1203. query_filter:
  1204. Look only for points which satisfies this conditions
  1205. search_params:
  1206. Additional search params
  1207. limit:
  1208. Max number of result to return
  1209. offset:
  1210. Offset of the first result to return. May be used to paginate results. Note: large offset values may cause performance issues.
  1211. with_payload:
  1212. Select which payload to return with the response. Default: None
  1213. with_vectors:
  1214. Whether to return the point vector with the result?
  1215. using:
  1216. Define which vector to use for recommendation, if not specified - try to use default vector.
  1217. lookup_from:
  1218. The location used to lookup vectors. If not specified - use current collection. Note: the other collection should have the same vector size as the current collection.
  1219. consistency:
  1220. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1221. - int - number of replicas to query, values should present in all queried replicas
  1222. - 'majority' - query all replicas, but return values present in the majority of replicas
  1223. - 'quorum' - query the majority of replicas, return values present in all of them
  1224. - 'all' - query all replicas, and return values present in all replicas
  1225. shard_key_selector:
  1226. This parameter allows to specify which shards should be queried.
  1227. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1228. timeout:
  1229. Overrides global timeout for this search. Unit is seconds.
  1230. Returns:
  1231. List of discovered points with discovery or context scores, accordingly.
  1232. """
  1233. warnings.warn(
  1234. "`discover` method is deprecated and will be removed in the future."
  1235. " Use `query_points` instead.",
  1236. DeprecationWarning,
  1237. stacklevel=2,
  1238. )
  1239. return self._client.discover(
  1240. collection_name=collection_name,
  1241. target=target,
  1242. context=context,
  1243. query_filter=query_filter,
  1244. search_params=search_params,
  1245. limit=limit,
  1246. offset=offset,
  1247. with_payload=with_payload,
  1248. with_vectors=with_vectors,
  1249. using=using,
  1250. lookup_from=lookup_from,
  1251. consistency=consistency,
  1252. shard_key_selector=shard_key_selector,
  1253. timeout=timeout,
  1254. **kwargs,
  1255. )
  1256. def discover_batch(
  1257. self,
  1258. collection_name: str,
  1259. requests: Sequence[types.DiscoverRequest],
  1260. consistency: Optional[types.ReadConsistency] = None,
  1261. timeout: Optional[int] = None,
  1262. **kwargs: Any,
  1263. ) -> list[list[types.ScoredPoint]]:
  1264. warnings.warn(
  1265. "`discover_batch` method is deprecated and will be removed in the future."
  1266. " Use `query_batch_points` instead.",
  1267. DeprecationWarning,
  1268. stacklevel=2,
  1269. )
  1270. return self._client.discover_batch(
  1271. collection_name=collection_name,
  1272. requests=requests,
  1273. consistency=consistency,
  1274. timeout=timeout,
  1275. **kwargs,
  1276. )
  1277. def scroll(
  1278. self,
  1279. collection_name: str,
  1280. scroll_filter: Optional[types.Filter] = None,
  1281. limit: int = 10,
  1282. order_by: Optional[types.OrderBy] = None,
  1283. offset: Optional[types.PointId] = None,
  1284. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  1285. with_vectors: Union[bool, Sequence[str]] = False,
  1286. consistency: Optional[types.ReadConsistency] = None,
  1287. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1288. timeout: Optional[int] = None,
  1289. **kwargs: Any,
  1290. ) -> tuple[list[types.Record], Optional[types.PointId]]:
  1291. """Scroll over all (matching) points in the collection.
  1292. This method provides a way to iterate over all stored points with some optional filtering condition.
  1293. Scroll does not apply any similarity estimations, it will return points sorted by id in ascending order.
  1294. Args:
  1295. collection_name: Name of the collection
  1296. scroll_filter: If provided - only returns points matching filtering conditions
  1297. limit: How many points to return
  1298. order_by: Order the records by a payload key. If `None` - order by id
  1299. offset: If provided - skip points with ids less than given `offset`
  1300. with_payload:
  1301. - Specify which stored payload should be attached to the result.
  1302. - If `True` - attach all payload
  1303. - If `False` - do not attach any payload
  1304. - If List of string - include only specified fields
  1305. - If `PayloadSelector` - use explicit rules
  1306. with_vectors:
  1307. - If `True` - Attach stored vector to the search result.
  1308. - If `False` (default) - Do not attach vector.
  1309. - If List of string - include only specified fields
  1310. consistency:
  1311. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1312. - int - number of replicas to query, values should present in all queried replicas
  1313. - 'majority' - query all replicas, but return values present in the majority of replicas
  1314. - 'quorum' - query the majority of replicas, return values present in all of them
  1315. - 'all' - query all replicas, and return values present in all replicas
  1316. shard_key_selector:
  1317. This parameter allows to specify which shards should be queried.
  1318. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1319. timeout:
  1320. Overrides global timeout for this operation. Unit is seconds.
  1321. Returns:
  1322. A pair of (List of points) and (optional offset for the next scroll request).
  1323. If next page offset is `None` - there is no more points in the collection to scroll.
  1324. """
  1325. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1326. return self._client.scroll(
  1327. collection_name=collection_name,
  1328. scroll_filter=scroll_filter,
  1329. limit=limit,
  1330. order_by=order_by,
  1331. offset=offset,
  1332. with_payload=with_payload,
  1333. with_vectors=with_vectors,
  1334. consistency=consistency,
  1335. shard_key_selector=shard_key_selector,
  1336. timeout=timeout,
  1337. **kwargs,
  1338. )
  1339. def count(
  1340. self,
  1341. collection_name: str,
  1342. count_filter: Optional[types.Filter] = None,
  1343. exact: bool = True,
  1344. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1345. timeout: Optional[int] = None,
  1346. **kwargs: Any,
  1347. ) -> types.CountResult:
  1348. """Count points in the collection.
  1349. Count points in the collection matching the given filter.
  1350. Args:
  1351. collection_name: name of the collection to count points in
  1352. count_filter: filtering conditions
  1353. exact:
  1354. If `True` - provide the exact count of points matching the filter.
  1355. If `False` - provide the approximate count of points matching the filter. Works faster.
  1356. shard_key_selector:
  1357. This parameter allows to specify which shards should be queried.
  1358. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1359. timeout:
  1360. Overrides global timeout for this operation. Unit is seconds.
  1361. Returns:
  1362. Amount of points in the collection matching the filter.
  1363. """
  1364. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1365. return self._client.count(
  1366. collection_name=collection_name,
  1367. count_filter=count_filter,
  1368. exact=exact,
  1369. shard_key_selector=shard_key_selector,
  1370. timeout=timeout,
  1371. **kwargs,
  1372. )
  1373. def facet(
  1374. self,
  1375. collection_name: str,
  1376. key: str,
  1377. facet_filter: Optional[types.Filter] = None,
  1378. limit: int = 10,
  1379. exact: bool = False,
  1380. consistency: Optional[types.ReadConsistency] = None,
  1381. timeout: Optional[int] = None,
  1382. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1383. **kwargs: Any,
  1384. ) -> types.FacetResponse:
  1385. """Facet counts for the collection. For a specific payload key, returns unique values along with their counts.
  1386. Higher counts come first in the results.
  1387. Args:
  1388. collection_name: Name of the collection
  1389. key: Payload field to facet
  1390. facet_filter: Filter to apply
  1391. limit: Maximum number of hits to return
  1392. exact: If `True` - provide the exact count of points matching the filter. If `False` - provide the approximate count of points matching the filter. Works faster.
  1393. consistency:
  1394. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1395. - int - number of replicas to query, values should present in all queried replicas
  1396. - 'majority' - query all replicas, but return values present in the majority of replicas
  1397. - 'quorum' - query the majority of replicas, return values present in all of them
  1398. - 'all' - query all replicas, and return values present in all replicas
  1399. timeout: Overrides global timeout for this search. Unit is seconds.
  1400. shard_key_selector:
  1401. This parameter allows to specify which shards should be queried.
  1402. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1403. Returns:
  1404. Unique values in the facet and the amount of points that they cover.
  1405. """
  1406. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1407. return self._client.facet(
  1408. collection_name=collection_name,
  1409. key=key,
  1410. facet_filter=facet_filter,
  1411. limit=limit,
  1412. exact=exact,
  1413. consistency=consistency,
  1414. timeout=timeout,
  1415. shard_key_selector=shard_key_selector,
  1416. **kwargs,
  1417. )
  1418. def upsert(
  1419. self,
  1420. collection_name: str,
  1421. points: types.Points,
  1422. wait: bool = True,
  1423. ordering: Optional[types.WriteOrdering] = None,
  1424. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1425. **kwargs: Any,
  1426. ) -> types.UpdateResult:
  1427. """
  1428. Update or insert a new point into the collection.
  1429. If point with given ID already exists - it will be overwritten.
  1430. Args:
  1431. collection_name (str): To which collection to insert
  1432. points (Point): Batch or list of points to insert
  1433. wait (bool): Await for the results to be processed.
  1434. - If `true`, result will be returned only when all changes are applied
  1435. - If `false`, result will be returned immediately after the confirmation of receiving.
  1436. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1437. - `weak` (default) - write operations may be reordered, works faster
  1438. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1439. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1440. shard_key_selector:
  1441. Defines the shard groups that should be used to write updates into.
  1442. If multiple shard_keys are provided, the update will be written to each of them.
  1443. Only works for collections with `custom` sharding method.
  1444. Returns:
  1445. Operation Result(UpdateResult)
  1446. """
  1447. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1448. if (
  1449. not isinstance(points, types.Batch)
  1450. and len(points) > 0
  1451. and isinstance(points[0], grpc.PointStruct)
  1452. ):
  1453. # gRPC structures won't support local inference feature, so we deprecated it
  1454. show_warning_once(
  1455. message="""
  1456. Usage of `grpc.PointStruct` is deprecated. Please use `models.PointStruct` instead.
  1457. """,
  1458. category=DeprecationWarning,
  1459. idx="grpc-input",
  1460. stacklevel=4,
  1461. )
  1462. if not self.cloud_inference and self._inference_inspector.inspect(points):
  1463. if isinstance(points, types.Batch):
  1464. points = next(
  1465. iter(
  1466. self._embed_models(
  1467. points, is_query=False, batch_size=self.local_inference_batch_size
  1468. )
  1469. )
  1470. )
  1471. else:
  1472. points = list(
  1473. self._embed_models(
  1474. points, is_query=False, batch_size=self.local_inference_batch_size
  1475. )
  1476. )
  1477. return self._client.upsert(
  1478. collection_name=collection_name,
  1479. points=points,
  1480. wait=wait,
  1481. ordering=ordering,
  1482. shard_key_selector=shard_key_selector,
  1483. **kwargs,
  1484. )
  1485. def update_vectors(
  1486. self,
  1487. collection_name: str,
  1488. points: Sequence[types.PointVectors],
  1489. wait: bool = True,
  1490. ordering: Optional[types.WriteOrdering] = None,
  1491. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1492. **kwargs: Any,
  1493. ) -> types.UpdateResult:
  1494. """Update specified vectors in the collection. Keeps payload and unspecified vectors unchanged.
  1495. Args:
  1496. collection_name (str): Name of the collection to update vectors in
  1497. points (Point): List of (id, vector) pairs to update. Vector might be a list of numbers or a dict of named vectors.
  1498. Examples:
  1499. - `PointVectors(id=1, vector=[1, 2, 3])`
  1500. - `PointVectors(id=2, vector={'vector_1': [1, 2, 3], 'vector_2': [4, 5, 6]})`
  1501. wait (bool): Await for the results to be processed.
  1502. - If `true`, result will be returned only when all changes are applied
  1503. - If `false`, result will be returned immediately after the confirmation of receiving.
  1504. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1505. - `weak` (default) - write operations may be reordered, works faster
  1506. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1507. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1508. shard_key_selector:
  1509. Defines the shard groups that should be used to write updates into.
  1510. If multiple shard_keys are provided, the update will be written to each of them.
  1511. Only works for collections with `custom` sharding method.
  1512. Returns:
  1513. Operation Result(UpdateResult)
  1514. """
  1515. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1516. if not self.cloud_inference and self._inference_inspector.inspect(points):
  1517. points = list(
  1518. self._embed_models(
  1519. points, is_query=False, batch_size=self.local_inference_batch_size
  1520. )
  1521. )
  1522. return self._client.update_vectors(
  1523. collection_name=collection_name,
  1524. points=points,
  1525. wait=wait,
  1526. ordering=ordering,
  1527. shard_key_selector=shard_key_selector,
  1528. )
  1529. def delete_vectors(
  1530. self,
  1531. collection_name: str,
  1532. vectors: Sequence[str],
  1533. points: types.PointsSelector,
  1534. wait: bool = True,
  1535. ordering: Optional[types.WriteOrdering] = None,
  1536. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1537. **kwargs: Any,
  1538. ) -> types.UpdateResult:
  1539. """Delete specified vector from the collection. Does not affect payload.
  1540. Args:
  1541. collection_name (str): Name of the collection to delete vector from
  1542. vectors: List of names of the vectors to delete. Use `""` to delete the default vector. At least one vector should be specified.
  1543. points (Point): Selects points based on list of IDs or filter
  1544. Examples:
  1545. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1546. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1547. wait (bool): Await for the results to be processed.
  1548. - If `true`, result will be returned only when all changes are applied
  1549. - If `false`, result will be returned immediately after the confirmation of receiving.
  1550. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1551. - `weak` (default) - write operations may be reordered, works faster
  1552. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1553. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1554. shard_key_selector:
  1555. Defines the shard groups that should be used to write updates into.
  1556. If multiple shard_keys are provided, the update will be written to each of them.
  1557. Only works for collections with `custom` sharding method.
  1558. Returns:
  1559. Operation result
  1560. """
  1561. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1562. return self._client.delete_vectors(
  1563. collection_name=collection_name,
  1564. vectors=vectors,
  1565. points=points,
  1566. wait=wait,
  1567. ordering=ordering,
  1568. shard_key_selector=shard_key_selector,
  1569. )
  1570. def retrieve(
  1571. self,
  1572. collection_name: str,
  1573. ids: Sequence[types.PointId],
  1574. with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
  1575. with_vectors: Union[bool, Sequence[str]] = False,
  1576. consistency: Optional[types.ReadConsistency] = None,
  1577. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1578. timeout: Optional[int] = None,
  1579. **kwargs: Any,
  1580. ) -> list[types.Record]:
  1581. """Retrieve stored points by IDs
  1582. Args:
  1583. collection_name: Name of the collection to lookup in
  1584. ids: list of IDs to lookup
  1585. with_payload:
  1586. - Specify which stored payload should be attached to the result.
  1587. - If `True` - attach all payload
  1588. - If `False` - do not attach any payload
  1589. - If List of string - include only specified fields
  1590. - If `PayloadSelector` - use explicit rules
  1591. with_vectors:
  1592. - If `True` - Attach stored vector to the search result.
  1593. - If `False` - Do not attach vector.
  1594. - If List of string - Attach only specified vectors.
  1595. - Default: `False`
  1596. consistency:
  1597. Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
  1598. - int - number of replicas to query, values should present in all queried replicas
  1599. - 'majority' - query all replicas, but return values present in the majority of replicas
  1600. - 'quorum' - query the majority of replicas, return values present in all of them
  1601. - 'all' - query all replicas, and return values present in all replicas
  1602. shard_key_selector:
  1603. This parameter allows to specify which shards should be queried.
  1604. If `None` - query all shards. Only works for collections with `custom` sharding method.
  1605. timeout:
  1606. Overrides global timeout for this operation. Unit is seconds.
  1607. Returns:
  1608. List of points
  1609. """
  1610. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1611. return self._client.retrieve(
  1612. collection_name=collection_name,
  1613. ids=ids,
  1614. with_payload=with_payload,
  1615. with_vectors=with_vectors,
  1616. consistency=consistency,
  1617. shard_key_selector=shard_key_selector,
  1618. timeout=timeout,
  1619. **kwargs,
  1620. )
  1621. def delete(
  1622. self,
  1623. collection_name: str,
  1624. points_selector: types.PointsSelector,
  1625. wait: bool = True,
  1626. ordering: Optional[types.WriteOrdering] = None,
  1627. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1628. **kwargs: Any,
  1629. ) -> types.UpdateResult:
  1630. """Deletes selected points from collection
  1631. Args:
  1632. collection_name: Name of the collection
  1633. wait: Await for the results to be processed.
  1634. - If `true`, result will be returned only when all changes are applied
  1635. - If `false`, result will be returned immediately after the confirmation of receiving.
  1636. points_selector: Selects points based on list of IDs or filter.
  1637. Examples:
  1638. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1639. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1640. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1641. - `weak` (default) - write operations may be reordered, works faster
  1642. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1643. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1644. shard_key_selector:
  1645. Defines the shard groups that should be used to write updates into.
  1646. If multiple shard_keys are provided, the update will be written to each of them.
  1647. Only works for collections with `custom` sharding method.
  1648. Returns:
  1649. Operation result
  1650. """
  1651. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1652. return self._client.delete(
  1653. collection_name=collection_name,
  1654. points_selector=points_selector,
  1655. wait=wait,
  1656. ordering=ordering,
  1657. shard_key_selector=shard_key_selector,
  1658. **kwargs,
  1659. )
  1660. def set_payload(
  1661. self,
  1662. collection_name: str,
  1663. payload: types.Payload,
  1664. points: types.PointsSelector,
  1665. key: Optional[str] = None,
  1666. wait: bool = True,
  1667. ordering: Optional[types.WriteOrdering] = None,
  1668. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1669. **kwargs: Any,
  1670. ) -> types.UpdateResult:
  1671. """
  1672. Modifies payload of the specified points.
  1673. Examples:
  1674. `Set payload`::
  1675. # Assign payload value with key `"key"` to points 1, 2, 3.
  1676. # If payload value with specified key already exists - it will be overwritten
  1677. qdrant_client.set_payload(
  1678. collection_name="test_collection",
  1679. wait=True,
  1680. payload={
  1681. "key": "value"
  1682. },
  1683. points=[1, 2, 3]
  1684. )
  1685. Args:
  1686. collection_name: Name of the collection.
  1687. wait: Await for the results to be processed.
  1688. - If `true`, the result will be returned only when all changes are applied.
  1689. - If `false`, the result will be returned immediately after confirmation of receipt.
  1690. payload: Key-value pairs of payload to assign.
  1691. points: List of affected points, filter, or points selector.
  1692. Example:
  1693. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1694. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1695. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1696. - `weak` (default): Write operations may be reordered, works faster.
  1697. - `medium`: Write operations go through a dynamically selected leader, may be inconsistent for a short period of time in case of leader change.
  1698. - `strong`: Write operations go through the permanent leader, consistent, but may be unavailable if the leader is down.
  1699. shard_key_selector: Defines the shard groups that should be used to write updates into.
  1700. If multiple shard keys are provided, the update will be written to each of them.
  1701. Only works for collections with the `custom` sharding method.
  1702. key: Path to the nested field in the payload to modify. If not specified, modifies the root of the payload.
  1703. E.g.::
  1704. PointStruct(
  1705. id=42,
  1706. vector=[...],
  1707. payload={
  1708. "recipe": {
  1709. "fruits": {"apple": "100g"}
  1710. }
  1711. }
  1712. )
  1713. qdrant_client.set_payload(
  1714. ...,
  1715. payload={"cinnamon": "2g"},
  1716. key="recipe.fruits",
  1717. points=[42]
  1718. )
  1719. PointStruct(
  1720. id=42,
  1721. vector=[...],
  1722. payload={
  1723. "recipe": {
  1724. "fruits": {
  1725. "apple": "100g",
  1726. "cinnamon": "2g"
  1727. }
  1728. }
  1729. }
  1730. )
  1731. Returns:
  1732. Operation result.
  1733. """
  1734. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1735. return self._client.set_payload(
  1736. collection_name=collection_name,
  1737. payload=payload,
  1738. points=points,
  1739. wait=wait,
  1740. ordering=ordering,
  1741. shard_key_selector=shard_key_selector,
  1742. key=key,
  1743. **kwargs,
  1744. )
  1745. def overwrite_payload(
  1746. self,
  1747. collection_name: str,
  1748. payload: types.Payload,
  1749. points: types.PointsSelector,
  1750. wait: bool = True,
  1751. ordering: Optional[types.WriteOrdering] = None,
  1752. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1753. **kwargs: Any,
  1754. ) -> types.UpdateResult:
  1755. """Overwrites payload of the specified points
  1756. After this operation is applied, only the specified payload will be present in the point.
  1757. The existing payload, even if the key is not specified in the payload, will be deleted.
  1758. Examples:
  1759. `Set payload`::
  1760. # Overwrite payload value with key `"key"` to points 1, 2, 3.
  1761. # If any other valid payload value exists - it will be deleted
  1762. qdrant_client.overwrite_payload(
  1763. collection_name="test_collection",
  1764. wait=True,
  1765. payload={
  1766. "key": "value"
  1767. },
  1768. points=[1,2,3]
  1769. )
  1770. Args:
  1771. collection_name: Name of the collection
  1772. wait: Await for the results to be processed.
  1773. - If `true`, result will be returned only when all changes are applied
  1774. - If `false`, result will be returned immediately after the confirmation of receiving.
  1775. payload: Key-value pairs of payload to assign
  1776. points: List of affected points, filter or points selector.
  1777. Example:
  1778. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1779. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1780. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1781. - `weak` (default) - write operations may be reordered, works faster
  1782. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1783. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1784. shard_key_selector:
  1785. Defines the shard groups that should be used to write updates into.
  1786. If multiple shard_keys are provided, the update will be written to each of them.
  1787. Only works for collections with `custom` sharding method.
  1788. Returns:
  1789. Operation result
  1790. """
  1791. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1792. return self._client.overwrite_payload(
  1793. collection_name=collection_name,
  1794. payload=payload,
  1795. points=points,
  1796. wait=wait,
  1797. ordering=ordering,
  1798. shard_key_selector=shard_key_selector,
  1799. **kwargs,
  1800. )
  1801. def delete_payload(
  1802. self,
  1803. collection_name: str,
  1804. keys: Sequence[str],
  1805. points: types.PointsSelector,
  1806. wait: bool = True,
  1807. ordering: Optional[types.WriteOrdering] = None,
  1808. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1809. **kwargs: Any,
  1810. ) -> types.UpdateResult:
  1811. """Remove values from point's payload
  1812. Args:
  1813. collection_name: Name of the collection
  1814. wait: Await for the results to be processed.
  1815. - If `true`, result will be returned only when all changes are applied
  1816. - If `false`, result will be returned immediately after the confirmation of receiving.
  1817. keys: List of payload keys to remove
  1818. points: List of affected points, filter or points selector.
  1819. Example:
  1820. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1821. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1822. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1823. - `weak` (default) - write operations may be reordered, works faster
  1824. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1825. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is downn
  1826. shard_key_selector:
  1827. Defines the shard groups that should be used to write updates into.
  1828. If multiple shard_keys are provided, the update will be written to each of them.
  1829. Only works for collections with `custom` sharding method.
  1830. Returns:
  1831. Operation result
  1832. """
  1833. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1834. return self._client.delete_payload(
  1835. collection_name=collection_name,
  1836. keys=keys,
  1837. points=points,
  1838. wait=wait,
  1839. ordering=ordering,
  1840. shard_key_selector=shard_key_selector,
  1841. **kwargs,
  1842. )
  1843. def clear_payload(
  1844. self,
  1845. collection_name: str,
  1846. points_selector: types.PointsSelector,
  1847. wait: bool = True,
  1848. ordering: Optional[types.WriteOrdering] = None,
  1849. shard_key_selector: Optional[types.ShardKeySelector] = None,
  1850. **kwargs: Any,
  1851. ) -> types.UpdateResult:
  1852. """Delete all payload for selected points
  1853. Args:
  1854. collection_name: Name of the collection
  1855. wait: Await for the results to be processed.
  1856. - If `true`, result will be returned only when all changes are applied
  1857. - If `false`, result will be returned immediately after the confirmation of receiving.
  1858. points_selector: List of affected points, filter or points selector. Example:
  1859. - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
  1860. - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
  1861. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1862. - `weak` (default) - write operations may be reordered, works faster
  1863. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1864. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1865. shard_key_selector:
  1866. Defines the shard groups that should be used to write updates into.
  1867. If multiple shard_keys are provided, the update will be written to each of them.
  1868. Only works for collections with `custom` sharding method.
  1869. Returns:
  1870. Operation result
  1871. """
  1872. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1873. return self._client.clear_payload(
  1874. collection_name=collection_name,
  1875. points_selector=points_selector,
  1876. wait=wait,
  1877. ordering=ordering,
  1878. shard_key_selector=shard_key_selector,
  1879. **kwargs,
  1880. )
  1881. def batch_update_points(
  1882. self,
  1883. collection_name: str,
  1884. update_operations: Sequence[types.UpdateOperation],
  1885. wait: bool = True,
  1886. ordering: Optional[types.WriteOrdering] = None,
  1887. **kwargs: Any,
  1888. ) -> list[types.UpdateResult]:
  1889. """Batch update points in the collection.
  1890. Args:
  1891. collection_name: Name of the collection
  1892. update_operations: List of update operations
  1893. wait: Await for the results to be processed.
  1894. - If `true`, result will be returned only when all changes are applied
  1895. - If `false`, result will be returned immediately after the confirmation of receiving.
  1896. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  1897. - `weak` (default) - write operations may be reordered, works faster
  1898. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  1899. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  1900. Returns:
  1901. Operation results
  1902. """
  1903. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1904. if not self.cloud_inference and self._inference_inspector.inspect(update_operations):
  1905. update_operations = list(
  1906. self._embed_models(
  1907. update_operations, is_query=False, batch_size=self.local_inference_batch_size
  1908. )
  1909. )
  1910. return self._client.batch_update_points(
  1911. collection_name=collection_name,
  1912. update_operations=update_operations,
  1913. wait=wait,
  1914. ordering=ordering,
  1915. **kwargs,
  1916. )
  1917. def update_collection_aliases(
  1918. self,
  1919. change_aliases_operations: Sequence[types.AliasOperations],
  1920. timeout: Optional[int] = None,
  1921. **kwargs: Any,
  1922. ) -> bool:
  1923. """Operation for performing changes of collection aliases.
  1924. Alias changes are atomic, meaning that no collection modifications can happen between alias operations.
  1925. Args:
  1926. change_aliases_operations: List of operations to perform
  1927. timeout:
  1928. Wait for operation commit timeout in seconds.
  1929. If timeout is reached - request will return with service error.
  1930. Returns:
  1931. Operation result
  1932. """
  1933. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1934. return self._client.update_collection_aliases(
  1935. change_aliases_operations=change_aliases_operations,
  1936. timeout=timeout,
  1937. **kwargs,
  1938. )
  1939. def get_collection_aliases(
  1940. self, collection_name: str, **kwargs: Any
  1941. ) -> types.CollectionsAliasesResponse:
  1942. """Get collection aliases
  1943. Args:
  1944. collection_name: Name of the collection
  1945. Returns:
  1946. Collection aliases
  1947. """
  1948. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1949. return self._client.get_collection_aliases(collection_name=collection_name, **kwargs)
  1950. def get_aliases(self, **kwargs: Any) -> types.CollectionsAliasesResponse:
  1951. """Get all aliases
  1952. Returns:
  1953. All aliases of all collections
  1954. """
  1955. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1956. return self._client.get_aliases(**kwargs)
  1957. def get_collections(self, **kwargs: Any) -> types.CollectionsResponse:
  1958. """Get list name of all existing collections
  1959. Returns:
  1960. List of the collections
  1961. """
  1962. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1963. return self._client.get_collections(**kwargs)
  1964. def get_collection(self, collection_name: str, **kwargs: Any) -> types.CollectionInfo:
  1965. """Get detailed information about specified existing collection
  1966. Args:
  1967. collection_name: Name of the collection
  1968. Returns:
  1969. Detailed information about the collection
  1970. """
  1971. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1972. return self._client.get_collection(collection_name=collection_name, **kwargs)
  1973. def collection_exists(self, collection_name: str, **kwargs: Any) -> bool:
  1974. """Check whether collection already exists
  1975. Args:
  1976. collection_name: Name of the collection
  1977. Returns:
  1978. True if collection exists, False if not
  1979. """
  1980. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  1981. return self._client.collection_exists(collection_name=collection_name, **kwargs)
  1982. def update_collection(
  1983. self,
  1984. collection_name: str,
  1985. optimizers_config: Optional[types.OptimizersConfigDiff] = None,
  1986. collection_params: Optional[types.CollectionParamsDiff] = None,
  1987. vectors_config: Optional[types.VectorsConfigDiff] = None,
  1988. hnsw_config: Optional[types.HnswConfigDiff] = None,
  1989. quantization_config: Optional[types.QuantizationConfigDiff] = None,
  1990. timeout: Optional[int] = None,
  1991. sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
  1992. strict_mode_config: Optional[types.StrictModeConfig] = None,
  1993. **kwargs: Any,
  1994. ) -> bool:
  1995. """Update parameters of the collection
  1996. Args:
  1997. collection_name: Name of the collection
  1998. optimizers_config: Override for optimizer configuration
  1999. collection_params: Override for collection parameters
  2000. vectors_config: Override for vector-specific configuration
  2001. hnsw_config: Override for HNSW index params
  2002. quantization_config: Override for quantization params
  2003. timeout:
  2004. Wait for operation commit timeout in seconds.
  2005. If timeout is reached - request will return with service error.
  2006. sparse_vectors_config: Override for sparse vector-specific configuration
  2007. strict_mode_config: Override for strict mode configuration
  2008. Returns:
  2009. Operation result
  2010. """
  2011. if "optimizer_config" in kwargs and optimizers_config is not None:
  2012. raise ValueError(
  2013. "Only one of optimizer_config and optimizers_config should be specified"
  2014. )
  2015. if "optimizer_config" in kwargs:
  2016. optimizers_config = kwargs.pop("optimizer_config")
  2017. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2018. return self._client.update_collection(
  2019. collection_name=collection_name,
  2020. optimizers_config=optimizers_config,
  2021. collection_params=collection_params,
  2022. vectors_config=vectors_config,
  2023. hnsw_config=hnsw_config,
  2024. quantization_config=quantization_config,
  2025. timeout=timeout,
  2026. sparse_vectors_config=sparse_vectors_config,
  2027. strict_mode_config=strict_mode_config,
  2028. **kwargs,
  2029. )
  2030. def delete_collection(
  2031. self, collection_name: str, timeout: Optional[int] = None, **kwargs: Any
  2032. ) -> bool:
  2033. """Removes collection and all it's data
  2034. Args:
  2035. collection_name: Name of the collection to delete
  2036. timeout:
  2037. Wait for operation commit timeout in seconds.
  2038. If timeout is reached - request will return with service error.
  2039. Returns:
  2040. Operation result
  2041. """
  2042. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2043. return self._client.delete_collection(
  2044. collection_name=collection_name, timeout=timeout, **kwargs
  2045. )
  2046. def create_collection(
  2047. self,
  2048. collection_name: str,
  2049. vectors_config: Optional[
  2050. Union[types.VectorParams, Mapping[str, types.VectorParams]]
  2051. ] = None,
  2052. sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
  2053. shard_number: Optional[int] = None,
  2054. sharding_method: Optional[types.ShardingMethod] = None,
  2055. replication_factor: Optional[int] = None,
  2056. write_consistency_factor: Optional[int] = None,
  2057. on_disk_payload: Optional[bool] = None,
  2058. hnsw_config: Optional[types.HnswConfigDiff] = None,
  2059. optimizers_config: Optional[types.OptimizersConfigDiff] = None,
  2060. wal_config: Optional[types.WalConfigDiff] = None,
  2061. quantization_config: Optional[types.QuantizationConfig] = None,
  2062. init_from: Optional[types.InitFrom] = None,
  2063. timeout: Optional[int] = None,
  2064. strict_mode_config: Optional[types.StrictModeConfig] = None,
  2065. **kwargs: Any,
  2066. ) -> bool:
  2067. """Create empty collection with given parameters
  2068. Args:
  2069. collection_name: Name of the collection to recreate
  2070. vectors_config:
  2071. Configuration of the vector storage. Vector params contains size and distance for the vector storage.
  2072. If dict is passed, service will create a vector storage for each key in the dict.
  2073. If single VectorParams is passed, service will create a single anonymous vector storage.
  2074. sparse_vectors_config:
  2075. Configuration of the sparse vector storage.
  2076. The service will create a sparse vector storage for each key in the dict.
  2077. shard_number: Number of shards in collection. Default is 1, minimum is 1.
  2078. sharding_method:
  2079. Defines strategy for shard creation.
  2080. Option `auto` (default) creates defined number of shards automatically.
  2081. Data will be distributed between shards automatically.
  2082. After creation, shards could be additionally replicated, but new shards could not be created.
  2083. Option `custom` allows to create shards manually, each shard should be created with assigned
  2084. unique `shard_key`. Data will be distributed between based on `shard_key` value.
  2085. replication_factor:
  2086. Replication factor for collection. Default is 1, minimum is 1.
  2087. Defines how many copies of each shard will be created.
  2088. Have effect only in distributed mode.
  2089. write_consistency_factor:
  2090. Write consistency factor for collection. Default is 1, minimum is 1.
  2091. Defines how many replicas should apply the operation for us to consider it successful.
  2092. Increasing this number will make the collection more resilient to inconsistencies, but will
  2093. also make it fail if not enough replicas are available.
  2094. Does not have any performance impact.
  2095. Have effect only in distributed mode.
  2096. on_disk_payload:
  2097. If true - point`s payload will not be stored in memory.
  2098. It will be read from the disk every time it is requested.
  2099. This setting saves RAM by (slightly) increasing the response time.
  2100. Note: those payload values that are involved in filtering and are indexed - remain in RAM.
  2101. hnsw_config: Params for HNSW index
  2102. optimizers_config: Params for optimizer
  2103. wal_config: Params for Write-Ahead-Log
  2104. quantization_config: Params for quantization, if None - quantization will be disabled
  2105. init_from: Use data stored in another collection to initialize this collection
  2106. timeout:
  2107. Wait for operation commit timeout in seconds.
  2108. If timeout is reached - request will return with service error.
  2109. strict_mode_config: Configure limitations for the collection, such as max size, rate limits, etc.
  2110. Returns:
  2111. Operation result
  2112. """
  2113. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2114. return self._client.create_collection(
  2115. collection_name=collection_name,
  2116. vectors_config=vectors_config,
  2117. shard_number=shard_number,
  2118. sharding_method=sharding_method,
  2119. replication_factor=replication_factor,
  2120. write_consistency_factor=write_consistency_factor,
  2121. on_disk_payload=on_disk_payload,
  2122. hnsw_config=hnsw_config,
  2123. optimizers_config=optimizers_config,
  2124. wal_config=wal_config,
  2125. quantization_config=quantization_config,
  2126. init_from=init_from,
  2127. timeout=timeout,
  2128. sparse_vectors_config=sparse_vectors_config,
  2129. strict_mode_config=strict_mode_config,
  2130. **kwargs,
  2131. )
  2132. # WARN: This method is deprecated and will be removed in the future
  2133. # Use separate check for collection existence and `create_collection` instead
  2134. def recreate_collection(
  2135. self,
  2136. collection_name: str,
  2137. vectors_config: Union[types.VectorParams, Mapping[str, types.VectorParams]],
  2138. sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
  2139. shard_number: Optional[int] = None,
  2140. sharding_method: Optional[types.ShardingMethod] = None,
  2141. replication_factor: Optional[int] = None,
  2142. write_consistency_factor: Optional[int] = None,
  2143. on_disk_payload: Optional[bool] = None,
  2144. hnsw_config: Optional[types.HnswConfigDiff] = None,
  2145. optimizers_config: Optional[types.OptimizersConfigDiff] = None,
  2146. wal_config: Optional[types.WalConfigDiff] = None,
  2147. quantization_config: Optional[types.QuantizationConfig] = None,
  2148. init_from: Optional[types.InitFrom] = None,
  2149. timeout: Optional[int] = None,
  2150. strict_mode_config: Optional[types.StrictModeConfig] = None,
  2151. **kwargs: Any,
  2152. ) -> bool:
  2153. """Delete and create empty collection with given parameters
  2154. Args:
  2155. collection_name: Name of the collection to recreate
  2156. vectors_config:
  2157. Configuration of the vector storage. Vector params contains size and distance for the vector storage.
  2158. If dict is passed, service will create a vector storage for each key in the dict.
  2159. If single VectorParams is passed, service will create a single anonymous vector storage.
  2160. sparse_vectors_config:
  2161. Configuration of the sparse vector storage.
  2162. The service will create a sparse vector storage for each key in the dict.
  2163. shard_number: Number of shards in collection. Default is 1, minimum is 1.
  2164. sharding_method:
  2165. Defines strategy for shard creation.
  2166. Option `auto` (default) creates defined number of shards automatically.
  2167. Data will be distributed between shards automatically.
  2168. After creation, shards could be additionally replicated, but new shards could not be created.
  2169. Option `custom` allows to create shards manually, each shard should be created with assigned
  2170. unique `shard_key`. Data will be distributed between based on `shard_key` value.
  2171. replication_factor:
  2172. Replication factor for collection. Default is 1, minimum is 1.
  2173. Defines how many copies of each shard will be created.
  2174. Have effect only in distributed mode.
  2175. write_consistency_factor:
  2176. Write consistency factor for collection. Default is 1, minimum is 1.
  2177. Defines how many replicas should apply the operation for us to consider it successful.
  2178. Increasing this number will make the collection more resilient to inconsistencies, but will
  2179. also make it fail if not enough replicas are available.
  2180. Does not have any performance impact.
  2181. Have effect only in distributed mode.
  2182. on_disk_payload:
  2183. If true - point`s payload will not be stored in memory.
  2184. It will be read from the disk every time it is requested.
  2185. This setting saves RAM by (slightly) increasing the response time.
  2186. Note: those payload values that are involved in filtering and are indexed - remain in RAM.
  2187. hnsw_config: Params for HNSW index
  2188. optimizers_config: Params for optimizer
  2189. wal_config: Params for Write-Ahead-Log
  2190. quantization_config: Params for quantization, if None - quantization will be disabled
  2191. init_from: Use data stored in another collection to initialize this collection
  2192. timeout:
  2193. Wait for operation commit timeout in seconds.
  2194. If timeout is reached - request will return with service error.
  2195. strict_mode_config: Configure limitations for the collection, such as max size, rate limits, etc.
  2196. Returns:
  2197. Operation result
  2198. """
  2199. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2200. warnings.warn(
  2201. "`recreate_collection` method is deprecated and will be removed in the future."
  2202. " Use `collection_exists` to check collection existence and `create_collection` instead.",
  2203. DeprecationWarning,
  2204. stacklevel=2,
  2205. )
  2206. return self._client.recreate_collection(
  2207. collection_name=collection_name,
  2208. vectors_config=vectors_config,
  2209. shard_number=shard_number,
  2210. sharding_method=sharding_method,
  2211. replication_factor=replication_factor,
  2212. write_consistency_factor=write_consistency_factor,
  2213. on_disk_payload=on_disk_payload,
  2214. hnsw_config=hnsw_config,
  2215. optimizers_config=optimizers_config,
  2216. wal_config=wal_config,
  2217. quantization_config=quantization_config,
  2218. init_from=init_from,
  2219. timeout=timeout,
  2220. sparse_vectors_config=sparse_vectors_config,
  2221. strict_mode_config=strict_mode_config,
  2222. **kwargs,
  2223. )
  2224. def upload_records(
  2225. self,
  2226. collection_name: str,
  2227. records: Iterable[types.Record],
  2228. batch_size: int = 64,
  2229. parallel: int = 1,
  2230. method: Optional[str] = None,
  2231. max_retries: int = 3,
  2232. wait: bool = False,
  2233. shard_key_selector: Optional[types.ShardKeySelector] = None,
  2234. **kwargs: Any,
  2235. ) -> None:
  2236. """Upload records to the collection
  2237. Similar to `upload_collection` method, but operates with records, rather than vector and payload individually.
  2238. Args:
  2239. collection_name: Name of the collection to upload to
  2240. records: Iterator over records to upload
  2241. batch_size: How many vectors upload per-request, Default: 64
  2242. parallel: Number of parallel processes of upload
  2243. method: Start method for parallel processes, Default: forkserver
  2244. max_retries: maximum number of retries in case of a failure
  2245. during the upload of a batch
  2246. wait:
  2247. Await for the results to be applied on the server side.
  2248. If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
  2249. If `false`, each update request will return immediately after the confirmation of receiving.
  2250. Default: `false`
  2251. shard_key_selector: Defines the shard groups that should be used to write updates into.
  2252. If multiple shard_keys are provided, the update will be written to each of them.
  2253. Only works for collections with `custom` sharding method.
  2254. This parameter overwrites shard keys written in the records.
  2255. """
  2256. warnings.warn(
  2257. "`upload_records` is deprecated, use `upload_points` instead",
  2258. DeprecationWarning,
  2259. stacklevel=2,
  2260. )
  2261. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2262. return self._client.upload_records(
  2263. collection_name=collection_name,
  2264. records=records,
  2265. batch_size=batch_size,
  2266. parallel=parallel,
  2267. method=method,
  2268. max_retries=max_retries,
  2269. wait=wait,
  2270. shard_key_selector=shard_key_selector,
  2271. )
  2272. def upload_points(
  2273. self,
  2274. collection_name: str,
  2275. points: Iterable[types.PointStruct],
  2276. batch_size: int = 64,
  2277. parallel: int = 1,
  2278. method: Optional[str] = None,
  2279. max_retries: int = 3,
  2280. wait: bool = False,
  2281. shard_key_selector: Optional[types.ShardKeySelector] = None,
  2282. **kwargs: Any,
  2283. ) -> None:
  2284. """Upload points to the collection
  2285. Similar to `upload_collection` method, but operates with points, rather than vector and payload individually.
  2286. Args:
  2287. collection_name: Name of the collection to upload to
  2288. points: Iterator over points to upload
  2289. batch_size: How many vectors upload per-request, Default: 64
  2290. parallel: Number of parallel processes of upload
  2291. method: Start method for parallel processes, Default: forkserver
  2292. max_retries: maximum number of retries in case of a failure
  2293. during the upload of a batch
  2294. wait:
  2295. Await for the results to be applied on the server side.
  2296. If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
  2297. If `false`, each update request will return immediately after the confirmation of receiving.
  2298. Default: `false`
  2299. shard_key_selector: Defines the shard groups that should be used to write updates into.
  2300. If multiple shard_keys are provided, the update will be written to each of them.
  2301. Only works for collections with `custom` sharding method.
  2302. This parameter overwrites shard keys written in the records.
  2303. """
  2304. def chain(*iterables: Iterable) -> Iterable:
  2305. for iterable in iterables:
  2306. yield from iterable
  2307. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2308. if not self.cloud_inference:
  2309. iter_points = iter(points)
  2310. requires_inference = False
  2311. try:
  2312. point = next(iter_points)
  2313. requires_inference = self._inference_inspector.inspect(point)
  2314. points = chain(iter([point]), iter_points)
  2315. except (StopIteration, StopAsyncIteration):
  2316. points = []
  2317. if requires_inference:
  2318. points = self._embed_models_strict(
  2319. points, parallel=parallel, batch_size=self.local_inference_batch_size
  2320. )
  2321. return self._client.upload_points(
  2322. collection_name=collection_name,
  2323. points=points,
  2324. batch_size=batch_size,
  2325. parallel=parallel,
  2326. method=method,
  2327. max_retries=max_retries,
  2328. wait=wait,
  2329. shard_key_selector=shard_key_selector,
  2330. )
  2331. def upload_collection(
  2332. self,
  2333. collection_name: str,
  2334. vectors: Union[
  2335. Iterable[types.VectorStruct],
  2336. dict[str, types.NumpyArray],
  2337. types.NumpyArray,
  2338. ],
  2339. payload: Optional[Iterable[dict[Any, Any]]] = None,
  2340. ids: Optional[Iterable[types.PointId]] = None,
  2341. batch_size: int = 64,
  2342. parallel: int = 1,
  2343. method: Optional[str] = None,
  2344. max_retries: int = 3,
  2345. wait: bool = False,
  2346. shard_key_selector: Optional[types.ShardKeySelector] = None,
  2347. **kwargs: Any,
  2348. ) -> None:
  2349. """Upload vectors and payload to the collection.
  2350. This method will perform automatic batching of the data.
  2351. If you need to perform a single update, use `upsert` method.
  2352. Note: use `upload_records` method if you want to upload multiple vectors with single payload.
  2353. Args:
  2354. collection_name: Name of the collection to upload to
  2355. vectors: np.ndarray or an iterable over vectors to upload. Might be mmaped
  2356. payload: Iterable of vectors payload, Optional, Default: None
  2357. ids: Iterable of custom vectors ids, Optional, Default: None
  2358. batch_size: How many vectors upload per-request, Default: 64
  2359. parallel: Number of parallel processes of upload
  2360. method: Start method for parallel processes, Default: forkserver
  2361. max_retries: maximum number of retries in case of a failure
  2362. during the upload of a batch
  2363. wait:
  2364. Await for the results to be applied on the server side.
  2365. If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
  2366. If `false`, each update request will return immediately after the confirmation of receiving.
  2367. Default: `false`
  2368. shard_key_selector: Defines the shard groups that should be used to write updates into.
  2369. If multiple shard_keys are provided, the update will be written to each of them.
  2370. Only works for collections with `custom` sharding method.
  2371. """
  2372. def chain(*iterables: Iterable) -> Iterable:
  2373. for iterable in iterables:
  2374. yield from iterable
  2375. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2376. if not self.cloud_inference:
  2377. if not isinstance(vectors, dict) and not isinstance(vectors, np.ndarray):
  2378. requires_inference = False
  2379. try:
  2380. iter_vectors = iter(vectors)
  2381. vector = next(iter_vectors)
  2382. requires_inference = self._inference_inspector.inspect(vector)
  2383. vectors = chain(iter([vector]), iter_vectors)
  2384. except (StopIteration, StopAsyncIteration):
  2385. vectors = []
  2386. if requires_inference:
  2387. vectors = self._embed_models_strict(
  2388. vectors, parallel=parallel, batch_size=self.local_inference_batch_size
  2389. )
  2390. return self._client.upload_collection(
  2391. collection_name=collection_name,
  2392. vectors=vectors,
  2393. payload=payload,
  2394. ids=ids,
  2395. batch_size=batch_size,
  2396. parallel=parallel,
  2397. method=method,
  2398. max_retries=max_retries,
  2399. wait=wait,
  2400. shard_key_selector=shard_key_selector,
  2401. )
  2402. def create_payload_index(
  2403. self,
  2404. collection_name: str,
  2405. field_name: str,
  2406. field_schema: Optional[types.PayloadSchemaType] = None,
  2407. field_type: Optional[types.PayloadSchemaType] = None,
  2408. wait: bool = True,
  2409. ordering: Optional[types.WriteOrdering] = None,
  2410. **kwargs: Any,
  2411. ) -> types.UpdateResult:
  2412. """Creates index for a given payload field.
  2413. Indexed fields allow to perform filtered search operations faster.
  2414. Args:
  2415. collection_name: Name of the collection
  2416. field_name: Name of the payload field
  2417. field_schema: Type of data to index
  2418. field_type: Same as field_schema, but deprecated
  2419. wait: Await for the results to be processed.
  2420. - If `true`, result will be returned only when all changes are applied
  2421. - If `false`, result will be returned immediately after the confirmation of receiving.
  2422. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  2423. - `weak` (default) - write operations may be reordered, works faster
  2424. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  2425. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  2426. Returns:
  2427. Operation Result
  2428. """
  2429. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2430. return self._client.create_payload_index(
  2431. collection_name=collection_name,
  2432. field_name=field_name,
  2433. field_schema=field_schema,
  2434. field_type=field_type,
  2435. wait=wait,
  2436. ordering=ordering,
  2437. **kwargs,
  2438. )
  2439. def delete_payload_index(
  2440. self,
  2441. collection_name: str,
  2442. field_name: str,
  2443. wait: bool = True,
  2444. ordering: Optional[types.WriteOrdering] = None,
  2445. **kwargs: Any,
  2446. ) -> types.UpdateResult:
  2447. """Removes index for a given payload field.
  2448. Args:
  2449. collection_name: Name of the collection
  2450. field_name: Name of the payload field
  2451. wait: Await for the results to be processed.
  2452. - If `true`, result will be returned only when all changes are applied
  2453. - If `false`, result will be returned immediately after the confirmation of receiving.
  2454. ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
  2455. - `weak` (default) - write operations may be reordered, works faster
  2456. - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
  2457. - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
  2458. Returns:
  2459. Operation Result
  2460. """
  2461. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2462. return self._client.delete_payload_index(
  2463. collection_name=collection_name,
  2464. field_name=field_name,
  2465. wait=wait,
  2466. ordering=ordering,
  2467. **kwargs,
  2468. )
  2469. def list_snapshots(
  2470. self, collection_name: str, **kwargs: Any
  2471. ) -> list[types.SnapshotDescription]:
  2472. """List all snapshots for a given collection.
  2473. Args:
  2474. collection_name: Name of the collection
  2475. Returns:
  2476. List of snapshots
  2477. """
  2478. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2479. return self._client.list_snapshots(collection_name=collection_name, **kwargs)
  2480. def create_snapshot(
  2481. self, collection_name: str, wait: bool = True, **kwargs: Any
  2482. ) -> Optional[types.SnapshotDescription]:
  2483. """Create snapshot for a given collection.
  2484. Args:
  2485. collection_name: Name of the collection
  2486. wait: Await for the snapshot to be created.
  2487. - If `true`, result will be returned only when a snapshot is created
  2488. - If `false`, result will be returned immediately after the confirmation of receiving.
  2489. Returns:
  2490. Snapshot description
  2491. """
  2492. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2493. return self._client.create_snapshot(collection_name=collection_name, wait=wait, **kwargs)
  2494. def delete_snapshot(
  2495. self, collection_name: str, snapshot_name: str, wait: bool = True, **kwargs: Any
  2496. ) -> Optional[bool]:
  2497. """Delete snapshot for a given collection.
  2498. Args:
  2499. collection_name: Name of the collection
  2500. snapshot_name: Snapshot id
  2501. wait: Await for the snapshot to be deleted.
  2502. - If `true`, result will be returned only when the snapshot is deleted
  2503. - If `false`, result will be returned immediately after the confirmation of receiving.
  2504. Returns:
  2505. True if snapshot was deleted
  2506. """
  2507. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2508. return self._client.delete_snapshot(
  2509. collection_name=collection_name,
  2510. snapshot_name=snapshot_name,
  2511. wait=wait,
  2512. **kwargs,
  2513. )
  2514. def list_full_snapshots(self, **kwargs: Any) -> list[types.SnapshotDescription]:
  2515. """List all snapshots for a whole storage
  2516. Returns:
  2517. List of snapshots
  2518. """
  2519. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2520. return self._client.list_full_snapshots(**kwargs)
  2521. def create_full_snapshot(
  2522. self, wait: bool = True, **kwargs: Any
  2523. ) -> Optional[types.SnapshotDescription]:
  2524. """Create snapshot for a whole storage.
  2525. Args:
  2526. wait: Await for the snapshot to be created.
  2527. - If `true`, result will be returned only when the snapshot is created
  2528. - If `false`, result will be returned immediately after the confirmation of receiving.
  2529. Returns:
  2530. Snapshot description
  2531. """
  2532. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2533. return self._client.create_full_snapshot(wait=wait, **kwargs)
  2534. def delete_full_snapshot(
  2535. self, snapshot_name: str, wait: bool = True, **kwargs: Any
  2536. ) -> Optional[bool]:
  2537. """Delete snapshot for a whole storage.
  2538. Args:
  2539. snapshot_name: Snapshot name
  2540. wait: Await for the snapshot to be deleted.
  2541. - If `true`, result will be returned only when the snapshot is deleted
  2542. - If `false`, result will be returned immediately after the confirmation of receiving.
  2543. Returns:
  2544. True if snapshot was deleted
  2545. """
  2546. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2547. return self._client.delete_full_snapshot(snapshot_name=snapshot_name, wait=wait, **kwargs)
  2548. def recover_snapshot(
  2549. self,
  2550. collection_name: str,
  2551. location: str,
  2552. api_key: Optional[str] = None,
  2553. checksum: Optional[str] = None,
  2554. priority: Optional[types.SnapshotPriority] = None,
  2555. wait: bool = True,
  2556. **kwargs: Any,
  2557. ) -> Optional[bool]:
  2558. """Recover collection from snapshot.
  2559. Args:
  2560. collection_name: Name of the collection
  2561. location: URL of the snapshot
  2562. Example:
  2563. - URL `http://localhost:8080/collections/my_collection/snapshots/my_snapshot`
  2564. - Local path `file:///qdrant/snapshots/test_collection/test_collection-6194298859870377-2023-11-09-15-17-51.snapshot`
  2565. api_key: API key to use for accessing the snapshot on another server.
  2566. checksum: Checksum of the snapshot to verify the integrity of the snapshot.
  2567. priority: Defines source of truth for snapshot recovery
  2568. - `replica` (default) means - prefer existing data over the snapshot
  2569. - `no_sync` means - do not sync shard with other shards
  2570. - `snapshot` means - prefer snapshot data over the current state
  2571. wait: Await for the recovery to be done.
  2572. - If `true`, result will be returned only when the recovery is done
  2573. - If `false`, result will be returned immediately after the confirmation of receiving.
  2574. Returns:
  2575. True if snapshot was recovered
  2576. """
  2577. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2578. return self._client.recover_snapshot(
  2579. collection_name=collection_name,
  2580. location=location,
  2581. api_key=api_key,
  2582. checksum=checksum,
  2583. priority=priority,
  2584. wait=wait,
  2585. **kwargs,
  2586. )
  2587. def list_shard_snapshots(
  2588. self, collection_name: str, shard_id: int, **kwargs: Any
  2589. ) -> list[types.SnapshotDescription]:
  2590. """List all snapshots of a given shard
  2591. Args:
  2592. collection_name: Name of the collection
  2593. shard_id: Index of the shard
  2594. Returns:
  2595. List of snapshots
  2596. """
  2597. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2598. return self._client.list_shard_snapshots(
  2599. collection_name=collection_name,
  2600. shard_id=shard_id,
  2601. **kwargs,
  2602. )
  2603. def create_shard_snapshot(
  2604. self, collection_name: str, shard_id: int, wait: bool = True, **kwargs: Any
  2605. ) -> Optional[types.SnapshotDescription]:
  2606. """Create snapshot for a given shard.
  2607. Args:
  2608. collection_name: Name of the collection
  2609. shard_id: Index of the shard
  2610. wait: Await for the snapshot to be created.
  2611. - If `true`, result will be returned only when the snapshot is created.
  2612. - If `false`, result will be returned immediately after the confirmation of receiving.
  2613. Returns:
  2614. Snapshot description
  2615. """
  2616. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2617. return self._client.create_shard_snapshot(
  2618. collection_name=collection_name,
  2619. shard_id=shard_id,
  2620. wait=wait,
  2621. **kwargs,
  2622. )
  2623. def delete_shard_snapshot(
  2624. self,
  2625. collection_name: str,
  2626. shard_id: int,
  2627. snapshot_name: str,
  2628. wait: bool = True,
  2629. **kwargs: Any,
  2630. ) -> Optional[bool]:
  2631. """Delete snapshot for a given shard.
  2632. Args:
  2633. collection_name: Name of the collection
  2634. shard_id: Index of the shard
  2635. snapshot_name: Snapshot id
  2636. wait: Await for the snapshot to be deleted.
  2637. - If `true`, result will be returned only when the snapshot is deleted
  2638. - If `false`, result will be returned immediately after the confirmation of receiving.
  2639. Returns:
  2640. True if snapshot was deleted
  2641. """
  2642. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2643. return self._client.delete_shard_snapshot(
  2644. collection_name=collection_name,
  2645. shard_id=shard_id,
  2646. snapshot_name=snapshot_name,
  2647. wait=wait,
  2648. **kwargs,
  2649. )
  2650. def recover_shard_snapshot(
  2651. self,
  2652. collection_name: str,
  2653. shard_id: int,
  2654. location: str,
  2655. api_key: Optional[str] = None,
  2656. checksum: Optional[str] = None,
  2657. priority: Optional[types.SnapshotPriority] = None,
  2658. wait: bool = True,
  2659. **kwargs: Any,
  2660. ) -> Optional[bool]:
  2661. """Recover shard from snapshot.
  2662. Args:
  2663. collection_name: Name of the collection
  2664. shard_id: Index of the shard
  2665. location: URL of the snapshot
  2666. Example:
  2667. - URL `http://localhost:8080/collections/my_collection/snapshots/my_snapshot`
  2668. api_key: API key to use for accessing the snapshot on another server.
  2669. checksum: Checksum of the snapshot to verify the integrity of the snapshot.
  2670. priority: Defines source of truth for snapshot recovery
  2671. - `replica` (default) means - prefer existing data over the snapshot
  2672. - `no_sync` means - do not sync shard with other shards
  2673. - `snapshot` means - prefer snapshot data over the current state
  2674. wait: Await for the recovery to be done.
  2675. - If `true`, result will be returned only when the recovery is done
  2676. - If `false`, result will be returned immediately after the confirmation of receiving.
  2677. Returns:
  2678. True if snapshot was recovered
  2679. """
  2680. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2681. return self._client.recover_shard_snapshot(
  2682. collection_name=collection_name,
  2683. shard_id=shard_id,
  2684. location=location,
  2685. api_key=api_key,
  2686. checksum=checksum,
  2687. priority=priority,
  2688. wait=wait,
  2689. **kwargs,
  2690. )
  2691. def lock_storage(self, reason: str, **kwargs: Any) -> types.LocksOption:
  2692. """Lock storage for writing."""
  2693. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2694. return self._client.lock_storage(reason=reason, **kwargs)
  2695. def unlock_storage(self, **kwargs: Any) -> types.LocksOption:
  2696. """Unlock storage for writing."""
  2697. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2698. return self._client.unlock_storage(**kwargs)
  2699. def get_locks(self, **kwargs: Any) -> types.LocksOption:
  2700. """Get current locks state."""
  2701. assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
  2702. return self._client.get_locks(**kwargs)
  2703. def migrate(
  2704. self,
  2705. dest_client: QdrantBase,
  2706. collection_names: Optional[list[str]] = None,
  2707. batch_size: int = 100,
  2708. recreate_on_collision: bool = False,
  2709. ) -> None:
  2710. """Migrate data from one Qdrant instance to another.
  2711. Args:
  2712. dest_client: Destination Qdrant instance either in local or remote mode
  2713. collection_names: List of collection names to migrate. If None - migrate all collections
  2714. batch_size: Batch size to be in scroll and upsert operations during migration
  2715. recreate_on_collision: If True - recreate collection on destination if it already exists, otherwise
  2716. raise ValueError exception
  2717. """
  2718. migrate(
  2719. self,
  2720. dest_client,
  2721. collection_names=collection_names,
  2722. batch_size=batch_size,
  2723. recreate_on_collision=recreate_on_collision,
  2724. )
  2725. def create_shard_key(
  2726. self,
  2727. collection_name: str,
  2728. shard_key: types.ShardKey,
  2729. shards_number: Optional[int] = None,
  2730. replication_factor: Optional[int] = None,
  2731. placement: Optional[list[int]] = None,
  2732. **kwargs: Any,
  2733. ) -> bool:
  2734. """Create shard key for collection.
  2735. Only works for collections with `custom` sharding method.
  2736. Args:
  2737. collection_name: Name of the collection
  2738. shard_key: Shard key to create
  2739. shards_number: How many shards to create for this key
  2740. replication_factor: Replication factor for this key
  2741. placement: List of peers to place shards on. If None - place on all peers.
  2742. Returns:
  2743. Operation result
  2744. """
  2745. return self._client.create_shard_key(
  2746. collection_name=collection_name,
  2747. shard_key=shard_key,
  2748. shards_number=shards_number,
  2749. replication_factor=replication_factor,
  2750. placement=placement,
  2751. **kwargs,
  2752. )
  2753. def delete_shard_key(
  2754. self,
  2755. collection_name: str,
  2756. shard_key: types.ShardKey,
  2757. **kwargs: Any,
  2758. ) -> bool:
  2759. """Delete shard key for collection.
  2760. Only works for collections with `custom` sharding method.
  2761. Args:
  2762. collection_name: Name of the collection
  2763. shard_key: Shard key to delete
  2764. Returns:
  2765. Operation result
  2766. """
  2767. return self._client.delete_shard_key(
  2768. collection_name=collection_name,
  2769. shard_key=shard_key,
  2770. **kwargs,
  2771. )
  2772. def info(self) -> types.VersionInfo:
  2773. """Returns information about the running Qdrant instance like version and commit id
  2774. Returns:
  2775. Title, version and optionally commit info
  2776. """
  2777. return self._client.info()