| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163 |
- import warnings
- from typing import (
- Any,
- Awaitable,
- Callable,
- Iterable,
- Mapping,
- Optional,
- Sequence,
- Union,
- )
- import numpy as np
- from qdrant_client import grpc as grpc
- from qdrant_client.client_base import QdrantBase
- from qdrant_client.common.client_warnings import show_warning_once
- from qdrant_client.conversions import common_types as types
- from qdrant_client.embed.type_inspector import Inspector
- from qdrant_client.http import ApiClient, SyncApis
- from qdrant_client.local.qdrant_local import QdrantLocal
- from qdrant_client.migrate import migrate
- from qdrant_client.qdrant_fastembed import QdrantFastembedMixin
- from qdrant_client.qdrant_remote import QdrantRemote
- class QdrantClient(QdrantFastembedMixin):
- """Entry point to communicate with Qdrant service via REST or gRPC API.
- It combines interface classes and endpoint implementation.
- Additionally, it provides custom implementations for frequently used methods like initial collection upload.
- All methods in QdrantClient accept both gRPC and REST structures as an input.
- Conversion will be performed automatically.
- .. note::
- This module methods are wrappers around generated client code for gRPC and REST methods.
- If you need lower-level access to generated clients, use following properties:
- - :py:attr:`QdrantClient.grpc_points`
- - :py:attr:`QdrantClient.grpc_collections`
- - :py:attr:`QdrantClient.rest`
- .. note::
- If you need async, please consider using Async Implementations of QdrantClient.
- - :class:`qdrant_client.async_qdrant_client`
- Args:
- location:
- If `":memory:"` - use in-memory Qdrant instance.
- If `str` - use it as a `url` parameter.
- If `None` - use default values for `host` and `port`.
- url: either host or str of "Optional[scheme], host, Optional[port], Optional[prefix]".
- Default: `None`
- port: Port of the REST API interface. Default: 6333
- grpc_port: Port of the gRPC interface. Default: 6334
- prefer_grpc: If `true` - use gPRC interface whenever possible in custom methods.
- https: If `true` - use HTTPS(SSL) protocol. Default: `None`
- api_key: API key for authentication in Qdrant Cloud. Default: `None`
- prefix:
- If not `None` - add `prefix` to the REST URL path.
- Example: `service/v1` will result in `http://localhost:6333/service/v1/{qdrant-endpoint}` for REST API.
- Default: `None`
- timeout:
- Timeout for REST and gRPC API requests.
- Default: 5 seconds for REST and unlimited for gRPC
- host: Host name of Qdrant service. If url and host are None, set to 'localhost'.
- Default: `None`
- path: Persistence path for QdrantLocal. Default: `None`
- force_disable_check_same_thread:
- For QdrantLocal, force disable check_same_thread. Default: `False`
- Only use this if you can guarantee that you can resolve the thread safety outside QdrantClient.
- auth_token_provider: Callback function to get Bearer access token. If given, the function will be called before each request to get the token.
- check_compatibility: If `true` - check compatibility with the server version. Default: `true`
- **kwargs: Additional arguments passed directly into REST client initialization
- """
- def __init__(
- self,
- location: Optional[str] = None,
- url: Optional[str] = None,
- port: Optional[int] = 6333,
- grpc_port: int = 6334,
- prefer_grpc: bool = False,
- https: Optional[bool] = None,
- api_key: Optional[str] = None,
- prefix: Optional[str] = None,
- timeout: Optional[int] = None,
- host: Optional[str] = None,
- path: Optional[str] = None,
- force_disable_check_same_thread: bool = False,
- grpc_options: Optional[dict[str, Any]] = None,
- auth_token_provider: Optional[
- Union[Callable[[], str], Callable[[], Awaitable[str]]]
- ] = None,
- cloud_inference: bool = False,
- local_inference_batch_size: Optional[int] = None,
- check_compatibility: bool = True,
- **kwargs: Any,
- ):
- # Saving the init options to facilitate building AsyncQdrantClient from QdrantClient and vice versa.
- # Eg. AsyncQdrantClient(**sync_client.init_options) or QdrantClient(**async_client.init_options)
- self._init_options = {
- key: value
- for key, value in locals().items()
- if key not in ("self", "__class__", "kwargs")
- }
- self._init_options.update({k: v for k, v in kwargs.items()})
- self._inference_inspector = Inspector()
- super().__init__(
- parser=self._inference_inspector.parser, **kwargs
- ) # If we want to pass any kwargs to the parent class or ignore unexpected kwargs,
- # we will need to pop them from **kwargs. Otherwise, they might be passed to QdrantRemote as httpx kwargs.
- # Httpx has specific set of params, which it accepts and will raise an error if it receives any other params.
- self._client: QdrantBase
- if sum([param is not None for param in (location, url, host, path)]) > 1:
- raise ValueError(
- "Only one of <location>, <url>, <host> or <path> should be specified."
- )
- if location == ":memory:":
- self._client = QdrantLocal(
- location=location,
- force_disable_check_same_thread=force_disable_check_same_thread,
- )
- elif path is not None:
- self._client = QdrantLocal(
- location=path,
- force_disable_check_same_thread=force_disable_check_same_thread,
- )
- else:
- if location is not None and url is None:
- url = location
- self._client = QdrantRemote(
- url=url,
- port=port,
- grpc_port=grpc_port,
- prefer_grpc=prefer_grpc,
- https=https,
- api_key=api_key,
- prefix=prefix,
- timeout=timeout,
- host=host,
- grpc_options=grpc_options,
- auth_token_provider=auth_token_provider,
- check_compatibility=check_compatibility,
- **kwargs,
- )
- if isinstance(self._client, QdrantLocal) and cloud_inference:
- raise ValueError(
- "Cloud inference is not supported for local Qdrant, consider using FastEmbed or switch to Qdrant Cloud"
- )
- self.cloud_inference = cloud_inference
- self.local_inference_batch_size = local_inference_batch_size
- def __del__(self) -> None:
- self.close()
- def close(self, grpc_grace: Optional[float] = None, **kwargs: Any) -> None:
- """Closes the connection to Qdrant
- Args:
- grpc_grace: Grace period for gRPC connection close. Default: None
- """
- if hasattr(self, "_client"):
- self._client.close(grpc_grace=grpc_grace, **kwargs)
- @property
- def grpc_collections(self) -> grpc.CollectionsStub:
- """gRPC client for collections methods
- Returns:
- An instance of raw gRPC client, generated from Protobuf
- """
- if isinstance(self._client, QdrantRemote):
- return self._client.grpc_collections
- raise NotImplementedError(f"gRPC client is not supported for {type(self._client)}")
- @property
- def grpc_points(self) -> grpc.PointsStub:
- """gRPC client for points methods
- Returns:
- An instance of raw gRPC client, generated from Protobuf
- """
- if isinstance(self._client, QdrantRemote):
- return self._client.grpc_points
- raise NotImplementedError(f"gRPC client is not supported for {type(self._client)}")
- @property
- def rest(self) -> SyncApis[ApiClient]:
- """REST Client
- Returns:
- An instance of raw REST API client, generated from OpenAPI schema
- """
- warnings.warn(
- "The 'rest' property is deprecated and will be removed in a future version. Use `http` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- if isinstance(self._client, QdrantRemote):
- return self._client.rest
- raise NotImplementedError(f"REST client is not supported for {type(self._client)}")
- @property
- def http(self) -> SyncApis[ApiClient]:
- """REST Client
- Returns:
- An instance of raw REST API client, generated from OpenAPI schema
- """
- if isinstance(self._client, QdrantRemote):
- return self._client.http
- raise NotImplementedError(f"REST client is not supported for {type(self._client)}")
- @property
- def init_options(self) -> dict[str, Any]:
- """`__init__` Options
- Returns:
- A dictionary of options the client class was instantiated with
- """
- return self._init_options
- def search_batch(
- self,
- collection_name: str,
- requests: Sequence[types.SearchRequest],
- timeout: Optional[int] = None,
- consistency: Optional[types.ReadConsistency] = None,
- **kwargs: Any,
- ) -> list[list[types.ScoredPoint]]:
- """Perform multiple searches in a collection mitigating network overhead
- Args:
- collection_name: Name of the collection
- requests: List of search requests
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of search responses
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`search_batch` method is deprecated and will be removed in the future."
- " Use `query_batch_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.search_batch(
- collection_name=collection_name,
- requests=requests,
- consistency=consistency,
- timeout=timeout,
- **kwargs,
- )
- def search(
- self,
- collection_name: str,
- query_vector: Union[
- Sequence[float],
- tuple[str, list[float]],
- types.NamedVector,
- types.NamedSparseVector,
- types.NumpyArray,
- ],
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- offset: Optional[int] = None,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- score_threshold: Optional[float] = None,
- append_payload: bool = True,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[types.ScoredPoint]:
- """Search for closest vectors in collection taking into account filtering conditions
- Args:
- collection_name: Collection to search in
- query_vector:
- Search for vectors closest to this.
- Can be either a vector itself, or a named vector, or a named sparse vector, or a tuple of vector name and vector itself
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many results return
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- append_payload: Same as `with_payload`. Deprecated.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Examples:
- `Search with filter`::
- qdrant.search(
- collection_name="test_collection",
- query_vector=[1.0, 0.1, 0.2, 0.7],
- query_filter=Filter(
- must=[
- FieldCondition(
- key='color',
- range=Match(
- value="red"
- )
- )
- ]
- )
- )
- Returns:
- List of found close points with similarity scores.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`search` method is deprecated and will be removed in the future."
- " Use `query_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.search(
- collection_name=collection_name,
- query_vector=query_vector,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- offset=offset,
- with_payload=with_payload,
- with_vectors=with_vectors,
- score_threshold=score_threshold,
- append_payload=append_payload,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def query_batch_points(
- self,
- collection_name: str,
- requests: Sequence[types.QueryRequest],
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[types.QueryResponse]:
- """Perform any search, recommend, discovery, context search operations in batch, and mitigate network overhead
- Args:
- collection_name: Name of the collection
- requests: List of query requests
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of query responses
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- requests = self._resolve_query_batch_request(requests)
- if not self.cloud_inference and self._inference_inspector.inspect(requests):
- requests = list(
- self._embed_models(
- requests, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- return self._client.query_batch_points(
- collection_name=collection_name,
- requests=requests,
- consistency=consistency,
- timeout=timeout,
- **kwargs,
- )
- def query_points(
- self,
- collection_name: str,
- query: Union[
- types.PointId,
- list[float],
- list[list[float]],
- types.SparseVector,
- types.Query,
- types.NumpyArray,
- types.Document,
- types.Image,
- types.InferenceObject,
- None,
- ] = None,
- using: Optional[str] = None,
- prefetch: Union[types.Prefetch, list[types.Prefetch], None] = None,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- offset: Optional[int] = None,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- score_threshold: Optional[float] = None,
- lookup_from: Optional[types.LookupLocation] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> types.QueryResponse:
- """Universal endpoint to run any available operation, such as search, recommendation, discovery, context search.
- Args:
- collection_name: Collection to search in
- query:
- Query for the chosen search type operation.
- - If `str` - use string as UUID of the existing point as a search query.
- - If `int` - use integer as ID of the existing point as a search query.
- - If `list[float]` - use as a dense vector for nearest search.
- - If `list[list[float]]` - use as a multi-vector for nearest search.
- - If `SparseVector` - use as a sparse vector for nearest search.
- - If `Query` - use as a query for specific search type.
- - If `NumpyArray` - use as a dense vector for nearest search.
- - If `Document` - infer vector from the document text and use it for nearest search (requires `fastembed` package installed).
- - If `None` - return first `limit` points from the collection.
- prefetch: prefetch queries to make a selection of the data to be used with the main query
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many results return
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- using:
- Name of the vectors to use for query.
- If `None` - use default vectors or provided in named vector structures.
- lookup_from:
- Defines a location (collection and vector field name), used to lookup vectors for recommendations,
- discovery and context queries.
- If `None` - current collection will be used.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Examples:
- `Search for closest points with a filter`::
- qdrant.query(
- collection_name="test_collection",
- query=[1.0, 0.1, 0.2, 0.7],
- query_filter=Filter(
- must=[
- FieldCondition(
- key='color',
- range=Match(
- value="red"
- )
- )
- ]
- )
- )
- Returns:
- QueryResponse structure containing list of found close points with similarity scores.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- # If the query contains unprocessed documents, we need to embed them and
- # replace the original query with the embedded vectors.
- query = self._resolve_query(query)
- if not self.cloud_inference:
- if self._inference_inspector.inspect(query) or self._inference_inspector.inspect(
- prefetch
- ):
- query = (
- next(
- iter(
- self._embed_models(
- query, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- )
- if query is not None
- else None
- )
- if isinstance(prefetch, list):
- prefetch = list(
- self._embed_models(
- prefetch, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- else:
- prefetch = (
- next(
- iter(
- self._embed_models(
- prefetch,
- is_query=True,
- batch_size=self.local_inference_batch_size,
- )
- )
- )
- if prefetch is not None
- else None
- )
- return self._client.query_points(
- collection_name=collection_name,
- query=query,
- prefetch=prefetch,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- offset=offset,
- with_payload=with_payload,
- with_vectors=with_vectors,
- score_threshold=score_threshold,
- using=using,
- lookup_from=lookup_from,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def query_points_groups(
- self,
- collection_name: str,
- group_by: str,
- query: Union[
- types.PointId,
- list[float],
- list[list[float]],
- types.SparseVector,
- types.Query,
- types.NumpyArray,
- types.Document,
- types.Image,
- types.InferenceObject,
- None,
- ] = None,
- using: Optional[str] = None,
- prefetch: Union[types.Prefetch, list[types.Prefetch], None] = None,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- group_size: int = 3,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- score_threshold: Optional[float] = None,
- with_lookup: Optional[types.WithLookupInterface] = None,
- lookup_from: Optional[types.LookupLocation] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> types.GroupsResult:
- """Universal endpoint to group on any available operation, such as search, recommendation, discovery, context search.
- Args:
- collection_name: Collection to search in
- query:
- Query for the chosen search type operation.
- - If `str` - use string as UUID of the existing point as a search query.
- - If `int` - use integer as ID of the existing point as a search query.
- - If `list[float]` - use as a dense vector for nearest search.
- - If `list[list[float]]` - use as a multi-vector for nearest search.
- - If `SparseVector` - use as a sparse vector for nearest search.
- - If `Query` - use as a query for specific search type.
- - If `NumpyArray` - use as a dense vector for nearest search.
- - If `Document` - infer vector from the document text and use it for nearest search (requires `fastembed` package installed).
- - If `None` - return first `limit` points from the collection.
- prefetch: prefetch queries to make a selection of the data to be used with the main query
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many results return
- group_size: How many results return for each group
- group_by: Name of the payload field to group by. Field must be of type "keyword" or "integer".
- Nested fields are specified using dot notation, e.g. "nested_field.subfield".
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- using:
- Name of the vectors to use for query.
- If `None` - use default vectors or provided in named vector structures.
- with_lookup:
- Look for points in another collection using the group ids.
- If specified, each group will contain a record from the specified collection
- with the same id as the group id. In addition, the parameter allows to specify
- which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
- lookup_from:
- Defines a location (collection and vector field name), used to lookup vectors being referenced in the query as IDs.
- If `None` - current collection will be used.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Examples:
- `Search for closest points and group results`::
- qdrant.query_points_groups(
- collection_name="test_collection",
- query=[1.0, 0.1, 0.2, 0.7],
- group_by="color",
- group_size=3,
- )
- Returns:
- List of groups with not more than `group_size` hits in each group.
- Each group also contains an id of the group, which is the value of the payload field.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- # If the query contains unprocessed documents, we need to embed them and
- # replace the original query with the embedded vectors.
- query = self._resolve_query(query)
- if not self.cloud_inference:
- if self._inference_inspector.inspect(query) or self._inference_inspector.inspect(
- prefetch
- ):
- query = (
- next(
- iter(
- self._embed_models(
- query, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- )
- if query is not None
- else None
- )
- if isinstance(prefetch, list):
- prefetch = list(
- self._embed_models(
- prefetch, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- elif prefetch is not None:
- prefetch = next(
- iter(
- self._embed_models(
- prefetch, is_query=True, batch_size=self.local_inference_batch_size
- )
- )
- )
- return self._client.query_points_groups(
- collection_name=collection_name,
- query=query,
- prefetch=prefetch,
- query_filter=query_filter,
- search_params=search_params,
- group_by=group_by,
- limit=limit,
- group_size=group_size,
- with_payload=with_payload,
- with_vectors=with_vectors,
- score_threshold=score_threshold,
- using=using,
- with_lookup=with_lookup,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def search_groups(
- self,
- collection_name: str,
- query_vector: Union[
- Sequence[float],
- tuple[str, list[float]],
- types.NamedVector,
- types.NamedSparseVector,
- types.NumpyArray,
- ],
- group_by: str,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- group_size: int = 1,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- score_threshold: Optional[float] = None,
- with_lookup: Optional[types.WithLookupInterface] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> types.GroupsResult:
- """Search for closest vectors grouped by payload field.
- Searches best matches for query vector grouped by the value of payload field.
- Useful to obtain most relevant results for each category, deduplicate results,
- finding the best representation vector for the same entity.
- Args:
- collection_name: Collection to search in
- query_vector:
- Search for vectors closest to this.
- Can be either a vector itself, or a named vector, or a named sparse vector, or a tuple of vector name and vector itself
- group_by: Name of the payload field to group by.
- Field must be of type "keyword" or "integer".
- Nested fields are specified using dot notation, e.g. "nested_field.subfield".
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many groups return
- group_size: How many results return for each group
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold: Minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- with_lookup:
- Look for points in another collection using the group ids.
- If specified, each group will contain a record from the specified collection
- with the same id as the group id. In addition, the parameter allows to specify
- which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result.
- Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of groups with not more than `group_size` hits in each group.
- Each group also contains an id of the group, which is the value of the payload field.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`search_groups` method is deprecated and will be removed in the future."
- " Use `query_points_groups` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.search_groups(
- collection_name=collection_name,
- query_vector=query_vector,
- group_by=group_by,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- group_size=group_size,
- with_payload=with_payload,
- with_vectors=with_vectors,
- score_threshold=score_threshold,
- with_lookup=with_lookup,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def recommend_batch(
- self,
- collection_name: str,
- requests: Sequence[types.RecommendRequest],
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[list[types.ScoredPoint]]:
- """Perform multiple recommend requests in batch mode
- Args:
- collection_name: Name of the collection
- requests: List of recommend requests
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of recommend responses
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`recommend_batch` method is deprecated and will be removed in the future."
- " Use `query_batch_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.recommend_batch(
- collection_name=collection_name,
- requests=requests,
- consistency=consistency,
- timeout=timeout,
- **kwargs,
- )
- def recommend(
- self,
- collection_name: str,
- positive: Optional[Sequence[types.RecommendExample]] = None,
- negative: Optional[Sequence[types.RecommendExample]] = None,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- offset: int = 0,
- with_payload: Union[bool, list[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, list[str]] = False,
- score_threshold: Optional[float] = None,
- using: Optional[str] = None,
- lookup_from: Optional[types.LookupLocation] = None,
- strategy: Optional[types.RecommendStrategy] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[types.ScoredPoint]:
- """Recommend points: search for similar points based on already stored in Qdrant examples.
- Provide IDs of the stored points, and Qdrant will perform search based on already existing vectors.
- This functionality is especially useful for recommendation over existing collection of points.
- Args:
- collection_name: Collection to search in
- positive:
- List of stored point IDs or vectors, which should be used as reference for similarity search.
- If there is only one example - this request is equivalent to the regular search with vector of that
- point.
- If there are more than one example, Qdrant will attempt to search for similar to all of them.
- Recommendation for multiple vectors is experimental.
- Its behaviour may change depending on selected strategy.
- negative:
- List of stored point IDs or vectors, which should be dissimilar to the search result.
- Negative examples is an experimental functionality.
- Its behaviour may change depending on selected strategy.
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many results return
- offset:
- Offset of the first result to return.
- May be used to paginate results.
- Note: large offset values may cause performance issues.
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- using:
- Name of the vectors to use for recommendations.
- If `None` - use default vectors.
- lookup_from:
- Defines a location (collection and vector field name), used to lookup vectors for recommendations.
- If `None` - current collection will be used.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- strategy:
- Strategy to use for recommendation.
- Strategy defines how to combine multiple examples into a recommendation query.
- Possible values:
- - 'average_vector' - calculates average vector of all examples and uses it for search
- - 'best_score' - finds the result which is closer to positive examples and further from negative
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of recommended points with similarity scores.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`recommend` method is deprecated and will be removed in the future."
- " Use `query_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.recommend(
- collection_name=collection_name,
- positive=positive,
- negative=negative,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- offset=offset,
- with_payload=with_payload,
- with_vectors=with_vectors,
- score_threshold=score_threshold,
- using=using,
- lookup_from=lookup_from,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- strategy=strategy,
- timeout=timeout,
- **kwargs,
- )
- def search_matrix_pairs(
- self,
- collection_name: str,
- query_filter: Optional[types.Filter] = None,
- limit: int = 3,
- sample: int = 10,
- using: Optional[str] = None,
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.SearchMatrixPairsResponse:
- """
- Compute distance matrix for sampled points with a pair-based output format.
- Args:
- collection_name: Name of the collection.
- query_filter: Filter to apply.
- limit: How many neighbors per sample to find.
- sample: How many points to select and search within.
- using: Name of the vectors to use for search. If `None`, use default vectors.
- consistency: Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int: Number of replicas to query, values should be present in all queried replicas.
- - 'majority': Query all replicas, but return values present in the majority of replicas.
- - 'quorum': Query the majority of replicas, return values present in all of them.
- - 'all': Query all replicas, and return values present in all replicas.
- timeout: Overrides global timeout for this search. Unit is seconds.
- shard_key_selector: This parameter allows specifying which shards should be queried.
- If `None`, query all shards. Only works for collections with the `custom` sharding method.
- Returns:
- Distance matrix using a pair-based encoding.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.search_matrix_pairs(
- collection_name=collection_name,
- query_filter=query_filter,
- limit=limit,
- sample=sample,
- using=using,
- consistency=consistency,
- timeout=timeout,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def search_matrix_offsets(
- self,
- collection_name: str,
- query_filter: Optional[types.Filter] = None,
- limit: int = 3,
- sample: int = 10,
- using: Optional[str] = None,
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.SearchMatrixOffsetsResponse:
- """
- Compute distance matrix for sampled points with an offset-based output format.
- Args:
- collection_name: Name of the collection.
- query_filter: Filter to apply.
- limit: How many neighbors per sample to find.
- sample: How many points to select and search within.
- using: Name of the vectors to use for search. If `None`, use default vectors.
- consistency: Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int: Number of replicas to query, values should present in all queried replicas.
- - 'majority': Query all replicas, but return values present in the majority of replicas.
- - 'quorum': Query the majority of replicas, return values present in all of them.
- - 'all': Query all replicas and return values present in all replicas.
- timeout: Overrides global timeout for this search. Unit is seconds.
- shard_key_selector: This parameter allows specifying which shards should be queried.
- If `None`, query all shards. Only works for collections with the `custom` sharding method.
- Returns:
- Distance matrix using an offset-based encoding.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.search_matrix_offsets(
- collection_name=collection_name,
- query_filter=query_filter,
- limit=limit,
- sample=sample,
- using=using,
- consistency=consistency,
- timeout=timeout,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def recommend_groups(
- self,
- collection_name: str,
- group_by: str,
- positive: Optional[Sequence[types.RecommendExample]] = None,
- negative: Optional[Sequence[types.RecommendExample]] = None,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- group_size: int = 1,
- score_threshold: Optional[float] = None,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- using: Optional[str] = None,
- lookup_from: Optional[types.LookupLocation] = None,
- with_lookup: Optional[types.WithLookupInterface] = None,
- strategy: Optional[types.RecommendStrategy] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> types.GroupsResult:
- """Recommend point groups: search for similar points based on already stored in Qdrant examples
- and groups by payload field.
- Recommend best matches for given stored examples grouped by the value of payload field.
- Useful to obtain most relevant results for each category, deduplicate results,
- finding the best representation vector for the same entity.
- Args:
- collection_name: Collection to search in
- positive:
- List of stored point IDs or vectors, which should be used as reference for similarity search.
- If there is only one example - this request is equivalent to the regular search with vector of that
- point.
- If there are more than one example, Qdrant will attempt to search for similar to all of them.
- Recommendation for multiple vectors is experimental.
- Its behaviour may change depending on selected strategy.
- negative:
- List of stored point IDs or vectors, which should be dissimilar to the search result.
- Negative examples is an experimental functionality.
- Its behaviour may change depending on selected strategy.
- group_by: Name of the payload field to group by.
- Field must be of type "keyword" or "integer".
- Nested fields are specified using dot notation, e.g. "nested_field.subfield".
- query_filter:
- - Exclude vectors which doesn't fit given conditions.
- - If `None` - search among all vectors
- search_params: Additional search params
- limit: How many groups return
- group_size: How many results return for each group
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - include only specified fields
- - Default: `False`
- score_threshold:
- Define a minimal score threshold for the result.
- If defined, less similar results will not be returned.
- Score of the returned result might be higher or smaller than the threshold depending
- on the Distance function used.
- E.g. for cosine similarity only higher scores will be returned.
- using:
- Name of the vectors to use for recommendations.
- If `None` - use default vectors.
- lookup_from:
- Defines a location (collection and vector field name), used to lookup vectors for recommendations.
- If `None` - current collection will be used.
- with_lookup:
- Look for points in another collection using the group ids.
- If specified, each group will contain a record from the specified collection
- with the same id as the group id. In addition, the parameter allows to specify
- which parts of the record should be returned, like in `with_payload` and `with_vectors` parameters.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- strategy:
- Strategy to use for recommendation.
- Strategy defines how to combine multiple examples into a recommendation query.
- Possible values:
- - 'average_vector' - calculates average vector of all examples and uses it for search
- - 'best_score' - finds the result which is closer to positive examples and further from negative
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of groups with not more than `group_size` hits in each group.
- Each group also contains an id of the group, which is the value of the payload field.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`recommend_groups` method is deprecated and will be removed in the future."
- " Use `query_points_groups` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.recommend_groups(
- collection_name=collection_name,
- group_by=group_by,
- positive=positive,
- negative=negative,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- group_size=group_size,
- score_threshold=score_threshold,
- with_payload=with_payload,
- with_vectors=with_vectors,
- using=using,
- lookup_from=lookup_from,
- with_lookup=with_lookup,
- strategy=strategy,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def discover(
- self,
- collection_name: str,
- target: Optional[types.TargetVector] = None,
- context: Optional[Sequence[types.ContextExamplePair]] = None,
- query_filter: Optional[types.Filter] = None,
- search_params: Optional[types.SearchParams] = None,
- limit: int = 10,
- offset: int = 0,
- with_payload: Union[bool, list[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, list[str]] = False,
- using: Optional[str] = None,
- lookup_from: Optional[types.LookupLocation] = None,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[types.ScoredPoint]:
- """
- Use context and a target to find the most similar points, constrained by the context.
- Args:
- collection_name: Collection to discover in
- target:
- Look for vectors closest to this.
- 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.
- context:
- Pairs of { positive, negative } examples to constrain the search.
- 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.
- 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.
- 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.
- query_filter:
- Look only for points which satisfies this conditions
- search_params:
- Additional search params
- limit:
- Max number of result to return
- offset:
- Offset of the first result to return. May be used to paginate results. Note: large offset values may cause performance issues.
- with_payload:
- Select which payload to return with the response. Default: None
- with_vectors:
- Whether to return the point vector with the result?
- using:
- Define which vector to use for recommendation, if not specified - try to use default vector.
- lookup_from:
- 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.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this search. Unit is seconds.
- Returns:
- List of discovered points with discovery or context scores, accordingly.
- """
- warnings.warn(
- "`discover` method is deprecated and will be removed in the future."
- " Use `query_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.discover(
- collection_name=collection_name,
- target=target,
- context=context,
- query_filter=query_filter,
- search_params=search_params,
- limit=limit,
- offset=offset,
- with_payload=with_payload,
- with_vectors=with_vectors,
- using=using,
- lookup_from=lookup_from,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def discover_batch(
- self,
- collection_name: str,
- requests: Sequence[types.DiscoverRequest],
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[list[types.ScoredPoint]]:
- warnings.warn(
- "`discover_batch` method is deprecated and will be removed in the future."
- " Use `query_batch_points` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.discover_batch(
- collection_name=collection_name,
- requests=requests,
- consistency=consistency,
- timeout=timeout,
- **kwargs,
- )
- def scroll(
- self,
- collection_name: str,
- scroll_filter: Optional[types.Filter] = None,
- limit: int = 10,
- order_by: Optional[types.OrderBy] = None,
- offset: Optional[types.PointId] = None,
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> tuple[list[types.Record], Optional[types.PointId]]:
- """Scroll over all (matching) points in the collection.
- This method provides a way to iterate over all stored points with some optional filtering condition.
- Scroll does not apply any similarity estimations, it will return points sorted by id in ascending order.
- Args:
- collection_name: Name of the collection
- scroll_filter: If provided - only returns points matching filtering conditions
- limit: How many points to return
- order_by: Order the records by a payload key. If `None` - order by id
- offset: If provided - skip points with ids less than given `offset`
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` (default) - Do not attach vector.
- - If List of string - include only specified fields
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this operation. Unit is seconds.
- Returns:
- A pair of (List of points) and (optional offset for the next scroll request).
- If next page offset is `None` - there is no more points in the collection to scroll.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.scroll(
- collection_name=collection_name,
- scroll_filter=scroll_filter,
- limit=limit,
- order_by=order_by,
- offset=offset,
- with_payload=with_payload,
- with_vectors=with_vectors,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def count(
- self,
- collection_name: str,
- count_filter: Optional[types.Filter] = None,
- exact: bool = True,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> types.CountResult:
- """Count points in the collection.
- Count points in the collection matching the given filter.
- Args:
- collection_name: name of the collection to count points in
- count_filter: filtering conditions
- 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.
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this operation. Unit is seconds.
- Returns:
- Amount of points in the collection matching the filter.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.count(
- collection_name=collection_name,
- count_filter=count_filter,
- exact=exact,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def facet(
- self,
- collection_name: str,
- key: str,
- facet_filter: Optional[types.Filter] = None,
- limit: int = 10,
- exact: bool = False,
- consistency: Optional[types.ReadConsistency] = None,
- timeout: Optional[int] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.FacetResponse:
- """Facet counts for the collection. For a specific payload key, returns unique values along with their counts.
- Higher counts come first in the results.
- Args:
- collection_name: Name of the collection
- key: Payload field to facet
- facet_filter: Filter to apply
- limit: Maximum number of hits to return
- 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.
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- timeout: Overrides global timeout for this search. Unit is seconds.
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- Returns:
- Unique values in the facet and the amount of points that they cover.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.facet(
- collection_name=collection_name,
- key=key,
- facet_filter=facet_filter,
- limit=limit,
- exact=exact,
- consistency=consistency,
- timeout=timeout,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def upsert(
- self,
- collection_name: str,
- points: types.Points,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """
- Update or insert a new point into the collection.
- If point with given ID already exists - it will be overwritten.
- Args:
- collection_name (str): To which collection to insert
- points (Point): Batch or list of points to insert
- wait (bool): Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation Result(UpdateResult)
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- if (
- not isinstance(points, types.Batch)
- and len(points) > 0
- and isinstance(points[0], grpc.PointStruct)
- ):
- # gRPC structures won't support local inference feature, so we deprecated it
- show_warning_once(
- message="""
- Usage of `grpc.PointStruct` is deprecated. Please use `models.PointStruct` instead.
- """,
- category=DeprecationWarning,
- idx="grpc-input",
- stacklevel=4,
- )
- if not self.cloud_inference and self._inference_inspector.inspect(points):
- if isinstance(points, types.Batch):
- points = next(
- iter(
- self._embed_models(
- points, is_query=False, batch_size=self.local_inference_batch_size
- )
- )
- )
- else:
- points = list(
- self._embed_models(
- points, is_query=False, batch_size=self.local_inference_batch_size
- )
- )
- return self._client.upsert(
- collection_name=collection_name,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def update_vectors(
- self,
- collection_name: str,
- points: Sequence[types.PointVectors],
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Update specified vectors in the collection. Keeps payload and unspecified vectors unchanged.
- Args:
- collection_name (str): Name of the collection to update vectors in
- points (Point): List of (id, vector) pairs to update. Vector might be a list of numbers or a dict of named vectors.
- Examples:
- - `PointVectors(id=1, vector=[1, 2, 3])`
- - `PointVectors(id=2, vector={'vector_1': [1, 2, 3], 'vector_2': [4, 5, 6]})`
- wait (bool): Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation Result(UpdateResult)
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- if not self.cloud_inference and self._inference_inspector.inspect(points):
- points = list(
- self._embed_models(
- points, is_query=False, batch_size=self.local_inference_batch_size
- )
- )
- return self._client.update_vectors(
- collection_name=collection_name,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- )
- def delete_vectors(
- self,
- collection_name: str,
- vectors: Sequence[str],
- points: types.PointsSelector,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Delete specified vector from the collection. Does not affect payload.
- Args:
- collection_name (str): Name of the collection to delete vector from
- vectors: List of names of the vectors to delete. Use `""` to delete the default vector. At least one vector should be specified.
- points (Point): Selects points based on list of IDs or filter
- Examples:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- wait (bool): Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_vectors(
- collection_name=collection_name,
- vectors=vectors,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- )
- def retrieve(
- self,
- collection_name: str,
- ids: Sequence[types.PointId],
- with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
- with_vectors: Union[bool, Sequence[str]] = False,
- consistency: Optional[types.ReadConsistency] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> list[types.Record]:
- """Retrieve stored points by IDs
- Args:
- collection_name: Name of the collection to lookup in
- ids: list of IDs to lookup
- with_payload:
- - Specify which stored payload should be attached to the result.
- - If `True` - attach all payload
- - If `False` - do not attach any payload
- - If List of string - include only specified fields
- - If `PayloadSelector` - use explicit rules
- with_vectors:
- - If `True` - Attach stored vector to the search result.
- - If `False` - Do not attach vector.
- - If List of string - Attach only specified vectors.
- - Default: `False`
- consistency:
- Read consistency of the search. Defines how many replicas should be queried before returning the result. Values:
- - int - number of replicas to query, values should present in all queried replicas
- - 'majority' - query all replicas, but return values present in the majority of replicas
- - 'quorum' - query the majority of replicas, return values present in all of them
- - 'all' - query all replicas, and return values present in all replicas
- shard_key_selector:
- This parameter allows to specify which shards should be queried.
- If `None` - query all shards. Only works for collections with `custom` sharding method.
- timeout:
- Overrides global timeout for this operation. Unit is seconds.
- Returns:
- List of points
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.retrieve(
- collection_name=collection_name,
- ids=ids,
- with_payload=with_payload,
- with_vectors=with_vectors,
- consistency=consistency,
- shard_key_selector=shard_key_selector,
- timeout=timeout,
- **kwargs,
- )
- def delete(
- self,
- collection_name: str,
- points_selector: types.PointsSelector,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Deletes selected points from collection
- Args:
- collection_name: Name of the collection
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- points_selector: Selects points based on list of IDs or filter.
- Examples:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete(
- collection_name=collection_name,
- points_selector=points_selector,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def set_payload(
- self,
- collection_name: str,
- payload: types.Payload,
- points: types.PointsSelector,
- key: Optional[str] = None,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """
- Modifies payload of the specified points.
- Examples:
- `Set payload`::
- # Assign payload value with key `"key"` to points 1, 2, 3.
- # If payload value with specified key already exists - it will be overwritten
- qdrant_client.set_payload(
- collection_name="test_collection",
- wait=True,
- payload={
- "key": "value"
- },
- points=[1, 2, 3]
- )
- Args:
- collection_name: Name of the collection.
- wait: Await for the results to be processed.
- - If `true`, the result will be returned only when all changes are applied.
- - If `false`, the result will be returned immediately after confirmation of receipt.
- payload: Key-value pairs of payload to assign.
- points: List of affected points, filter, or points selector.
- Example:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default): Write operations may be reordered, works faster.
- - `medium`: Write operations go through a dynamically selected leader, may be inconsistent for a short period of time in case of leader change.
- - `strong`: Write operations go through the permanent leader, consistent, but may be unavailable if the leader is down.
- shard_key_selector: Defines the shard groups that should be used to write updates into.
- If multiple shard keys are provided, the update will be written to each of them.
- Only works for collections with the `custom` sharding method.
- key: Path to the nested field in the payload to modify. If not specified, modifies the root of the payload.
- E.g.::
- PointStruct(
- id=42,
- vector=[...],
- payload={
- "recipe": {
- "fruits": {"apple": "100g"}
- }
- }
- )
- qdrant_client.set_payload(
- ...,
- payload={"cinnamon": "2g"},
- key="recipe.fruits",
- points=[42]
- )
- PointStruct(
- id=42,
- vector=[...],
- payload={
- "recipe": {
- "fruits": {
- "apple": "100g",
- "cinnamon": "2g"
- }
- }
- }
- )
- Returns:
- Operation result.
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.set_payload(
- collection_name=collection_name,
- payload=payload,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- key=key,
- **kwargs,
- )
- def overwrite_payload(
- self,
- collection_name: str,
- payload: types.Payload,
- points: types.PointsSelector,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Overwrites payload of the specified points
- After this operation is applied, only the specified payload will be present in the point.
- The existing payload, even if the key is not specified in the payload, will be deleted.
- Examples:
- `Set payload`::
- # Overwrite payload value with key `"key"` to points 1, 2, 3.
- # If any other valid payload value exists - it will be deleted
- qdrant_client.overwrite_payload(
- collection_name="test_collection",
- wait=True,
- payload={
- "key": "value"
- },
- points=[1,2,3]
- )
- Args:
- collection_name: Name of the collection
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- payload: Key-value pairs of payload to assign
- points: List of affected points, filter or points selector.
- Example:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.overwrite_payload(
- collection_name=collection_name,
- payload=payload,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def delete_payload(
- self,
- collection_name: str,
- keys: Sequence[str],
- points: types.PointsSelector,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Remove values from point's payload
- Args:
- collection_name: Name of the collection
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- keys: List of payload keys to remove
- points: List of affected points, filter or points selector.
- Example:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is downn
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_payload(
- collection_name=collection_name,
- keys=keys,
- points=points,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def clear_payload(
- self,
- collection_name: str,
- points_selector: types.PointsSelector,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Delete all payload for selected points
- Args:
- collection_name: Name of the collection
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- points_selector: List of affected points, filter or points selector. Example:
- - `points=[1, 2, 3, "cd3b53f0-11a7-449f-bc50-d06310e7ed90"]`
- - `points=Filter(must=[FieldCondition(key='rand_number', range=Range(gte=0.7))])`
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- shard_key_selector:
- Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.clear_payload(
- collection_name=collection_name,
- points_selector=points_selector,
- wait=wait,
- ordering=ordering,
- shard_key_selector=shard_key_selector,
- **kwargs,
- )
- def batch_update_points(
- self,
- collection_name: str,
- update_operations: Sequence[types.UpdateOperation],
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- **kwargs: Any,
- ) -> list[types.UpdateResult]:
- """Batch update points in the collection.
- Args:
- collection_name: Name of the collection
- update_operations: List of update operations
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- Returns:
- Operation results
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- if not self.cloud_inference and self._inference_inspector.inspect(update_operations):
- update_operations = list(
- self._embed_models(
- update_operations, is_query=False, batch_size=self.local_inference_batch_size
- )
- )
- return self._client.batch_update_points(
- collection_name=collection_name,
- update_operations=update_operations,
- wait=wait,
- ordering=ordering,
- **kwargs,
- )
- def update_collection_aliases(
- self,
- change_aliases_operations: Sequence[types.AliasOperations],
- timeout: Optional[int] = None,
- **kwargs: Any,
- ) -> bool:
- """Operation for performing changes of collection aliases.
- Alias changes are atomic, meaning that no collection modifications can happen between alias operations.
- Args:
- change_aliases_operations: List of operations to perform
- timeout:
- Wait for operation commit timeout in seconds.
- If timeout is reached - request will return with service error.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.update_collection_aliases(
- change_aliases_operations=change_aliases_operations,
- timeout=timeout,
- **kwargs,
- )
- def get_collection_aliases(
- self, collection_name: str, **kwargs: Any
- ) -> types.CollectionsAliasesResponse:
- """Get collection aliases
- Args:
- collection_name: Name of the collection
- Returns:
- Collection aliases
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.get_collection_aliases(collection_name=collection_name, **kwargs)
- def get_aliases(self, **kwargs: Any) -> types.CollectionsAliasesResponse:
- """Get all aliases
- Returns:
- All aliases of all collections
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.get_aliases(**kwargs)
- def get_collections(self, **kwargs: Any) -> types.CollectionsResponse:
- """Get list name of all existing collections
- Returns:
- List of the collections
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.get_collections(**kwargs)
- def get_collection(self, collection_name: str, **kwargs: Any) -> types.CollectionInfo:
- """Get detailed information about specified existing collection
- Args:
- collection_name: Name of the collection
- Returns:
- Detailed information about the collection
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.get_collection(collection_name=collection_name, **kwargs)
- def collection_exists(self, collection_name: str, **kwargs: Any) -> bool:
- """Check whether collection already exists
- Args:
- collection_name: Name of the collection
- Returns:
- True if collection exists, False if not
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.collection_exists(collection_name=collection_name, **kwargs)
- def update_collection(
- self,
- collection_name: str,
- optimizers_config: Optional[types.OptimizersConfigDiff] = None,
- collection_params: Optional[types.CollectionParamsDiff] = None,
- vectors_config: Optional[types.VectorsConfigDiff] = None,
- hnsw_config: Optional[types.HnswConfigDiff] = None,
- quantization_config: Optional[types.QuantizationConfigDiff] = None,
- timeout: Optional[int] = None,
- sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
- strict_mode_config: Optional[types.StrictModeConfig] = None,
- **kwargs: Any,
- ) -> bool:
- """Update parameters of the collection
- Args:
- collection_name: Name of the collection
- optimizers_config: Override for optimizer configuration
- collection_params: Override for collection parameters
- vectors_config: Override for vector-specific configuration
- hnsw_config: Override for HNSW index params
- quantization_config: Override for quantization params
- timeout:
- Wait for operation commit timeout in seconds.
- If timeout is reached - request will return with service error.
- sparse_vectors_config: Override for sparse vector-specific configuration
- strict_mode_config: Override for strict mode configuration
- Returns:
- Operation result
- """
- if "optimizer_config" in kwargs and optimizers_config is not None:
- raise ValueError(
- "Only one of optimizer_config and optimizers_config should be specified"
- )
- if "optimizer_config" in kwargs:
- optimizers_config = kwargs.pop("optimizer_config")
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.update_collection(
- collection_name=collection_name,
- optimizers_config=optimizers_config,
- collection_params=collection_params,
- vectors_config=vectors_config,
- hnsw_config=hnsw_config,
- quantization_config=quantization_config,
- timeout=timeout,
- sparse_vectors_config=sparse_vectors_config,
- strict_mode_config=strict_mode_config,
- **kwargs,
- )
- def delete_collection(
- self, collection_name: str, timeout: Optional[int] = None, **kwargs: Any
- ) -> bool:
- """Removes collection and all it's data
- Args:
- collection_name: Name of the collection to delete
- timeout:
- Wait for operation commit timeout in seconds.
- If timeout is reached - request will return with service error.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_collection(
- collection_name=collection_name, timeout=timeout, **kwargs
- )
- def create_collection(
- self,
- collection_name: str,
- vectors_config: Optional[
- Union[types.VectorParams, Mapping[str, types.VectorParams]]
- ] = None,
- sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
- shard_number: Optional[int] = None,
- sharding_method: Optional[types.ShardingMethod] = None,
- replication_factor: Optional[int] = None,
- write_consistency_factor: Optional[int] = None,
- on_disk_payload: Optional[bool] = None,
- hnsw_config: Optional[types.HnswConfigDiff] = None,
- optimizers_config: Optional[types.OptimizersConfigDiff] = None,
- wal_config: Optional[types.WalConfigDiff] = None,
- quantization_config: Optional[types.QuantizationConfig] = None,
- init_from: Optional[types.InitFrom] = None,
- timeout: Optional[int] = None,
- strict_mode_config: Optional[types.StrictModeConfig] = None,
- **kwargs: Any,
- ) -> bool:
- """Create empty collection with given parameters
- Args:
- collection_name: Name of the collection to recreate
- vectors_config:
- Configuration of the vector storage. Vector params contains size and distance for the vector storage.
- If dict is passed, service will create a vector storage for each key in the dict.
- If single VectorParams is passed, service will create a single anonymous vector storage.
- sparse_vectors_config:
- Configuration of the sparse vector storage.
- The service will create a sparse vector storage for each key in the dict.
- shard_number: Number of shards in collection. Default is 1, minimum is 1.
- sharding_method:
- Defines strategy for shard creation.
- Option `auto` (default) creates defined number of shards automatically.
- Data will be distributed between shards automatically.
- After creation, shards could be additionally replicated, but new shards could not be created.
- Option `custom` allows to create shards manually, each shard should be created with assigned
- unique `shard_key`. Data will be distributed between based on `shard_key` value.
- replication_factor:
- Replication factor for collection. Default is 1, minimum is 1.
- Defines how many copies of each shard will be created.
- Have effect only in distributed mode.
- write_consistency_factor:
- Write consistency factor for collection. Default is 1, minimum is 1.
- Defines how many replicas should apply the operation for us to consider it successful.
- Increasing this number will make the collection more resilient to inconsistencies, but will
- also make it fail if not enough replicas are available.
- Does not have any performance impact.
- Have effect only in distributed mode.
- on_disk_payload:
- If true - point`s payload will not be stored in memory.
- It will be read from the disk every time it is requested.
- This setting saves RAM by (slightly) increasing the response time.
- Note: those payload values that are involved in filtering and are indexed - remain in RAM.
- hnsw_config: Params for HNSW index
- optimizers_config: Params for optimizer
- wal_config: Params for Write-Ahead-Log
- quantization_config: Params for quantization, if None - quantization will be disabled
- init_from: Use data stored in another collection to initialize this collection
- timeout:
- Wait for operation commit timeout in seconds.
- If timeout is reached - request will return with service error.
- strict_mode_config: Configure limitations for the collection, such as max size, rate limits, etc.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.create_collection(
- collection_name=collection_name,
- vectors_config=vectors_config,
- shard_number=shard_number,
- sharding_method=sharding_method,
- replication_factor=replication_factor,
- write_consistency_factor=write_consistency_factor,
- on_disk_payload=on_disk_payload,
- hnsw_config=hnsw_config,
- optimizers_config=optimizers_config,
- wal_config=wal_config,
- quantization_config=quantization_config,
- init_from=init_from,
- timeout=timeout,
- sparse_vectors_config=sparse_vectors_config,
- strict_mode_config=strict_mode_config,
- **kwargs,
- )
- # WARN: This method is deprecated and will be removed in the future
- # Use separate check for collection existence and `create_collection` instead
- def recreate_collection(
- self,
- collection_name: str,
- vectors_config: Union[types.VectorParams, Mapping[str, types.VectorParams]],
- sparse_vectors_config: Optional[Mapping[str, types.SparseVectorParams]] = None,
- shard_number: Optional[int] = None,
- sharding_method: Optional[types.ShardingMethod] = None,
- replication_factor: Optional[int] = None,
- write_consistency_factor: Optional[int] = None,
- on_disk_payload: Optional[bool] = None,
- hnsw_config: Optional[types.HnswConfigDiff] = None,
- optimizers_config: Optional[types.OptimizersConfigDiff] = None,
- wal_config: Optional[types.WalConfigDiff] = None,
- quantization_config: Optional[types.QuantizationConfig] = None,
- init_from: Optional[types.InitFrom] = None,
- timeout: Optional[int] = None,
- strict_mode_config: Optional[types.StrictModeConfig] = None,
- **kwargs: Any,
- ) -> bool:
- """Delete and create empty collection with given parameters
- Args:
- collection_name: Name of the collection to recreate
- vectors_config:
- Configuration of the vector storage. Vector params contains size and distance for the vector storage.
- If dict is passed, service will create a vector storage for each key in the dict.
- If single VectorParams is passed, service will create a single anonymous vector storage.
- sparse_vectors_config:
- Configuration of the sparse vector storage.
- The service will create a sparse vector storage for each key in the dict.
- shard_number: Number of shards in collection. Default is 1, minimum is 1.
- sharding_method:
- Defines strategy for shard creation.
- Option `auto` (default) creates defined number of shards automatically.
- Data will be distributed between shards automatically.
- After creation, shards could be additionally replicated, but new shards could not be created.
- Option `custom` allows to create shards manually, each shard should be created with assigned
- unique `shard_key`. Data will be distributed between based on `shard_key` value.
- replication_factor:
- Replication factor for collection. Default is 1, minimum is 1.
- Defines how many copies of each shard will be created.
- Have effect only in distributed mode.
- write_consistency_factor:
- Write consistency factor for collection. Default is 1, minimum is 1.
- Defines how many replicas should apply the operation for us to consider it successful.
- Increasing this number will make the collection more resilient to inconsistencies, but will
- also make it fail if not enough replicas are available.
- Does not have any performance impact.
- Have effect only in distributed mode.
- on_disk_payload:
- If true - point`s payload will not be stored in memory.
- It will be read from the disk every time it is requested.
- This setting saves RAM by (slightly) increasing the response time.
- Note: those payload values that are involved in filtering and are indexed - remain in RAM.
- hnsw_config: Params for HNSW index
- optimizers_config: Params for optimizer
- wal_config: Params for Write-Ahead-Log
- quantization_config: Params for quantization, if None - quantization will be disabled
- init_from: Use data stored in another collection to initialize this collection
- timeout:
- Wait for operation commit timeout in seconds.
- If timeout is reached - request will return with service error.
- strict_mode_config: Configure limitations for the collection, such as max size, rate limits, etc.
- Returns:
- Operation result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- warnings.warn(
- "`recreate_collection` method is deprecated and will be removed in the future."
- " Use `collection_exists` to check collection existence and `create_collection` instead.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._client.recreate_collection(
- collection_name=collection_name,
- vectors_config=vectors_config,
- shard_number=shard_number,
- sharding_method=sharding_method,
- replication_factor=replication_factor,
- write_consistency_factor=write_consistency_factor,
- on_disk_payload=on_disk_payload,
- hnsw_config=hnsw_config,
- optimizers_config=optimizers_config,
- wal_config=wal_config,
- quantization_config=quantization_config,
- init_from=init_from,
- timeout=timeout,
- sparse_vectors_config=sparse_vectors_config,
- strict_mode_config=strict_mode_config,
- **kwargs,
- )
- def upload_records(
- self,
- collection_name: str,
- records: Iterable[types.Record],
- batch_size: int = 64,
- parallel: int = 1,
- method: Optional[str] = None,
- max_retries: int = 3,
- wait: bool = False,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> None:
- """Upload records to the collection
- Similar to `upload_collection` method, but operates with records, rather than vector and payload individually.
- Args:
- collection_name: Name of the collection to upload to
- records: Iterator over records to upload
- batch_size: How many vectors upload per-request, Default: 64
- parallel: Number of parallel processes of upload
- method: Start method for parallel processes, Default: forkserver
- max_retries: maximum number of retries in case of a failure
- during the upload of a batch
- wait:
- Await for the results to be applied on the server side.
- If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
- If `false`, each update request will return immediately after the confirmation of receiving.
- Default: `false`
- shard_key_selector: Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- This parameter overwrites shard keys written in the records.
- """
- warnings.warn(
- "`upload_records` is deprecated, use `upload_points` instead",
- DeprecationWarning,
- stacklevel=2,
- )
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.upload_records(
- collection_name=collection_name,
- records=records,
- batch_size=batch_size,
- parallel=parallel,
- method=method,
- max_retries=max_retries,
- wait=wait,
- shard_key_selector=shard_key_selector,
- )
- def upload_points(
- self,
- collection_name: str,
- points: Iterable[types.PointStruct],
- batch_size: int = 64,
- parallel: int = 1,
- method: Optional[str] = None,
- max_retries: int = 3,
- wait: bool = False,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> None:
- """Upload points to the collection
- Similar to `upload_collection` method, but operates with points, rather than vector and payload individually.
- Args:
- collection_name: Name of the collection to upload to
- points: Iterator over points to upload
- batch_size: How many vectors upload per-request, Default: 64
- parallel: Number of parallel processes of upload
- method: Start method for parallel processes, Default: forkserver
- max_retries: maximum number of retries in case of a failure
- during the upload of a batch
- wait:
- Await for the results to be applied on the server side.
- If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
- If `false`, each update request will return immediately after the confirmation of receiving.
- Default: `false`
- shard_key_selector: Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- This parameter overwrites shard keys written in the records.
- """
- def chain(*iterables: Iterable) -> Iterable:
- for iterable in iterables:
- yield from iterable
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- if not self.cloud_inference:
- iter_points = iter(points)
- requires_inference = False
- try:
- point = next(iter_points)
- requires_inference = self._inference_inspector.inspect(point)
- points = chain(iter([point]), iter_points)
- except (StopIteration, StopAsyncIteration):
- points = []
- if requires_inference:
- points = self._embed_models_strict(
- points, parallel=parallel, batch_size=self.local_inference_batch_size
- )
- return self._client.upload_points(
- collection_name=collection_name,
- points=points,
- batch_size=batch_size,
- parallel=parallel,
- method=method,
- max_retries=max_retries,
- wait=wait,
- shard_key_selector=shard_key_selector,
- )
- def upload_collection(
- self,
- collection_name: str,
- vectors: Union[
- Iterable[types.VectorStruct],
- dict[str, types.NumpyArray],
- types.NumpyArray,
- ],
- payload: Optional[Iterable[dict[Any, Any]]] = None,
- ids: Optional[Iterable[types.PointId]] = None,
- batch_size: int = 64,
- parallel: int = 1,
- method: Optional[str] = None,
- max_retries: int = 3,
- wait: bool = False,
- shard_key_selector: Optional[types.ShardKeySelector] = None,
- **kwargs: Any,
- ) -> None:
- """Upload vectors and payload to the collection.
- This method will perform automatic batching of the data.
- If you need to perform a single update, use `upsert` method.
- Note: use `upload_records` method if you want to upload multiple vectors with single payload.
- Args:
- collection_name: Name of the collection to upload to
- vectors: np.ndarray or an iterable over vectors to upload. Might be mmaped
- payload: Iterable of vectors payload, Optional, Default: None
- ids: Iterable of custom vectors ids, Optional, Default: None
- batch_size: How many vectors upload per-request, Default: 64
- parallel: Number of parallel processes of upload
- method: Start method for parallel processes, Default: forkserver
- max_retries: maximum number of retries in case of a failure
- during the upload of a batch
- wait:
- Await for the results to be applied on the server side.
- If `true`, each update request will explicitly wait for the confirmation of completion. Might be slower.
- If `false`, each update request will return immediately after the confirmation of receiving.
- Default: `false`
- shard_key_selector: Defines the shard groups that should be used to write updates into.
- If multiple shard_keys are provided, the update will be written to each of them.
- Only works for collections with `custom` sharding method.
- """
- def chain(*iterables: Iterable) -> Iterable:
- for iterable in iterables:
- yield from iterable
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- if not self.cloud_inference:
- if not isinstance(vectors, dict) and not isinstance(vectors, np.ndarray):
- requires_inference = False
- try:
- iter_vectors = iter(vectors)
- vector = next(iter_vectors)
- requires_inference = self._inference_inspector.inspect(vector)
- vectors = chain(iter([vector]), iter_vectors)
- except (StopIteration, StopAsyncIteration):
- vectors = []
- if requires_inference:
- vectors = self._embed_models_strict(
- vectors, parallel=parallel, batch_size=self.local_inference_batch_size
- )
- return self._client.upload_collection(
- collection_name=collection_name,
- vectors=vectors,
- payload=payload,
- ids=ids,
- batch_size=batch_size,
- parallel=parallel,
- method=method,
- max_retries=max_retries,
- wait=wait,
- shard_key_selector=shard_key_selector,
- )
- def create_payload_index(
- self,
- collection_name: str,
- field_name: str,
- field_schema: Optional[types.PayloadSchemaType] = None,
- field_type: Optional[types.PayloadSchemaType] = None,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Creates index for a given payload field.
- Indexed fields allow to perform filtered search operations faster.
- Args:
- collection_name: Name of the collection
- field_name: Name of the payload field
- field_schema: Type of data to index
- field_type: Same as field_schema, but deprecated
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- Returns:
- Operation Result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.create_payload_index(
- collection_name=collection_name,
- field_name=field_name,
- field_schema=field_schema,
- field_type=field_type,
- wait=wait,
- ordering=ordering,
- **kwargs,
- )
- def delete_payload_index(
- self,
- collection_name: str,
- field_name: str,
- wait: bool = True,
- ordering: Optional[types.WriteOrdering] = None,
- **kwargs: Any,
- ) -> types.UpdateResult:
- """Removes index for a given payload field.
- Args:
- collection_name: Name of the collection
- field_name: Name of the payload field
- wait: Await for the results to be processed.
- - If `true`, result will be returned only when all changes are applied
- - If `false`, result will be returned immediately after the confirmation of receiving.
- ordering (Optional[WriteOrdering]): Define strategy for ordering of the points. Possible values:
- - `weak` (default) - write operations may be reordered, works faster
- - `medium` - write operations go through dynamically selected leader, may be inconsistent for a short period of time in case of leader change
- - `strong` - Write operations go through the permanent leader, consistent, but may be unavailable if leader is down
- Returns:
- Operation Result
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_payload_index(
- collection_name=collection_name,
- field_name=field_name,
- wait=wait,
- ordering=ordering,
- **kwargs,
- )
- def list_snapshots(
- self, collection_name: str, **kwargs: Any
- ) -> list[types.SnapshotDescription]:
- """List all snapshots for a given collection.
- Args:
- collection_name: Name of the collection
- Returns:
- List of snapshots
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.list_snapshots(collection_name=collection_name, **kwargs)
- def create_snapshot(
- self, collection_name: str, wait: bool = True, **kwargs: Any
- ) -> Optional[types.SnapshotDescription]:
- """Create snapshot for a given collection.
- Args:
- collection_name: Name of the collection
- wait: Await for the snapshot to be created.
- - If `true`, result will be returned only when a snapshot is created
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- Snapshot description
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.create_snapshot(collection_name=collection_name, wait=wait, **kwargs)
- def delete_snapshot(
- self, collection_name: str, snapshot_name: str, wait: bool = True, **kwargs: Any
- ) -> Optional[bool]:
- """Delete snapshot for a given collection.
- Args:
- collection_name: Name of the collection
- snapshot_name: Snapshot id
- wait: Await for the snapshot to be deleted.
- - If `true`, result will be returned only when the snapshot is deleted
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- True if snapshot was deleted
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_snapshot(
- collection_name=collection_name,
- snapshot_name=snapshot_name,
- wait=wait,
- **kwargs,
- )
- def list_full_snapshots(self, **kwargs: Any) -> list[types.SnapshotDescription]:
- """List all snapshots for a whole storage
- Returns:
- List of snapshots
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.list_full_snapshots(**kwargs)
- def create_full_snapshot(
- self, wait: bool = True, **kwargs: Any
- ) -> Optional[types.SnapshotDescription]:
- """Create snapshot for a whole storage.
- Args:
- wait: Await for the snapshot to be created.
- - If `true`, result will be returned only when the snapshot is created
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- Snapshot description
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.create_full_snapshot(wait=wait, **kwargs)
- def delete_full_snapshot(
- self, snapshot_name: str, wait: bool = True, **kwargs: Any
- ) -> Optional[bool]:
- """Delete snapshot for a whole storage.
- Args:
- snapshot_name: Snapshot name
- wait: Await for the snapshot to be deleted.
- - If `true`, result will be returned only when the snapshot is deleted
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- True if snapshot was deleted
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_full_snapshot(snapshot_name=snapshot_name, wait=wait, **kwargs)
- def recover_snapshot(
- self,
- collection_name: str,
- location: str,
- api_key: Optional[str] = None,
- checksum: Optional[str] = None,
- priority: Optional[types.SnapshotPriority] = None,
- wait: bool = True,
- **kwargs: Any,
- ) -> Optional[bool]:
- """Recover collection from snapshot.
- Args:
- collection_name: Name of the collection
- location: URL of the snapshot
- Example:
- - URL `http://localhost:8080/collections/my_collection/snapshots/my_snapshot`
- - Local path `file:///qdrant/snapshots/test_collection/test_collection-6194298859870377-2023-11-09-15-17-51.snapshot`
- api_key: API key to use for accessing the snapshot on another server.
- checksum: Checksum of the snapshot to verify the integrity of the snapshot.
- priority: Defines source of truth for snapshot recovery
- - `replica` (default) means - prefer existing data over the snapshot
- - `no_sync` means - do not sync shard with other shards
- - `snapshot` means - prefer snapshot data over the current state
- wait: Await for the recovery to be done.
- - If `true`, result will be returned only when the recovery is done
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- True if snapshot was recovered
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.recover_snapshot(
- collection_name=collection_name,
- location=location,
- api_key=api_key,
- checksum=checksum,
- priority=priority,
- wait=wait,
- **kwargs,
- )
- def list_shard_snapshots(
- self, collection_name: str, shard_id: int, **kwargs: Any
- ) -> list[types.SnapshotDescription]:
- """List all snapshots of a given shard
- Args:
- collection_name: Name of the collection
- shard_id: Index of the shard
- Returns:
- List of snapshots
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.list_shard_snapshots(
- collection_name=collection_name,
- shard_id=shard_id,
- **kwargs,
- )
- def create_shard_snapshot(
- self, collection_name: str, shard_id: int, wait: bool = True, **kwargs: Any
- ) -> Optional[types.SnapshotDescription]:
- """Create snapshot for a given shard.
- Args:
- collection_name: Name of the collection
- shard_id: Index of the shard
- wait: Await for the snapshot to be created.
- - If `true`, result will be returned only when the snapshot is created.
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- Snapshot description
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.create_shard_snapshot(
- collection_name=collection_name,
- shard_id=shard_id,
- wait=wait,
- **kwargs,
- )
- def delete_shard_snapshot(
- self,
- collection_name: str,
- shard_id: int,
- snapshot_name: str,
- wait: bool = True,
- **kwargs: Any,
- ) -> Optional[bool]:
- """Delete snapshot for a given shard.
- Args:
- collection_name: Name of the collection
- shard_id: Index of the shard
- snapshot_name: Snapshot id
- wait: Await for the snapshot to be deleted.
- - If `true`, result will be returned only when the snapshot is deleted
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- True if snapshot was deleted
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.delete_shard_snapshot(
- collection_name=collection_name,
- shard_id=shard_id,
- snapshot_name=snapshot_name,
- wait=wait,
- **kwargs,
- )
- def recover_shard_snapshot(
- self,
- collection_name: str,
- shard_id: int,
- location: str,
- api_key: Optional[str] = None,
- checksum: Optional[str] = None,
- priority: Optional[types.SnapshotPriority] = None,
- wait: bool = True,
- **kwargs: Any,
- ) -> Optional[bool]:
- """Recover shard from snapshot.
- Args:
- collection_name: Name of the collection
- shard_id: Index of the shard
- location: URL of the snapshot
- Example:
- - URL `http://localhost:8080/collections/my_collection/snapshots/my_snapshot`
- api_key: API key to use for accessing the snapshot on another server.
- checksum: Checksum of the snapshot to verify the integrity of the snapshot.
- priority: Defines source of truth for snapshot recovery
- - `replica` (default) means - prefer existing data over the snapshot
- - `no_sync` means - do not sync shard with other shards
- - `snapshot` means - prefer snapshot data over the current state
- wait: Await for the recovery to be done.
- - If `true`, result will be returned only when the recovery is done
- - If `false`, result will be returned immediately after the confirmation of receiving.
- Returns:
- True if snapshot was recovered
- """
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.recover_shard_snapshot(
- collection_name=collection_name,
- shard_id=shard_id,
- location=location,
- api_key=api_key,
- checksum=checksum,
- priority=priority,
- wait=wait,
- **kwargs,
- )
- def lock_storage(self, reason: str, **kwargs: Any) -> types.LocksOption:
- """Lock storage for writing."""
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.lock_storage(reason=reason, **kwargs)
- def unlock_storage(self, **kwargs: Any) -> types.LocksOption:
- """Unlock storage for writing."""
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.unlock_storage(**kwargs)
- def get_locks(self, **kwargs: Any) -> types.LocksOption:
- """Get current locks state."""
- assert len(kwargs) == 0, f"Unknown arguments: {list(kwargs.keys())}"
- return self._client.get_locks(**kwargs)
- def migrate(
- self,
- dest_client: QdrantBase,
- collection_names: Optional[list[str]] = None,
- batch_size: int = 100,
- recreate_on_collision: bool = False,
- ) -> None:
- """Migrate data from one Qdrant instance to another.
- Args:
- dest_client: Destination Qdrant instance either in local or remote mode
- collection_names: List of collection names to migrate. If None - migrate all collections
- batch_size: Batch size to be in scroll and upsert operations during migration
- recreate_on_collision: If True - recreate collection on destination if it already exists, otherwise
- raise ValueError exception
- """
- migrate(
- self,
- dest_client,
- collection_names=collection_names,
- batch_size=batch_size,
- recreate_on_collision=recreate_on_collision,
- )
- def create_shard_key(
- self,
- collection_name: str,
- shard_key: types.ShardKey,
- shards_number: Optional[int] = None,
- replication_factor: Optional[int] = None,
- placement: Optional[list[int]] = None,
- **kwargs: Any,
- ) -> bool:
- """Create shard key for collection.
- Only works for collections with `custom` sharding method.
- Args:
- collection_name: Name of the collection
- shard_key: Shard key to create
- shards_number: How many shards to create for this key
- replication_factor: Replication factor for this key
- placement: List of peers to place shards on. If None - place on all peers.
- Returns:
- Operation result
- """
- return self._client.create_shard_key(
- collection_name=collection_name,
- shard_key=shard_key,
- shards_number=shards_number,
- replication_factor=replication_factor,
- placement=placement,
- **kwargs,
- )
- def delete_shard_key(
- self,
- collection_name: str,
- shard_key: types.ShardKey,
- **kwargs: Any,
- ) -> bool:
- """Delete shard key for collection.
- Only works for collections with `custom` sharding method.
- Args:
- collection_name: Name of the collection
- shard_key: Shard key to delete
- Returns:
- Operation result
- """
- return self._client.delete_shard_key(
- collection_name=collection_name,
- shard_key=shard_key,
- **kwargs,
- )
- def info(self) -> types.VersionInfo:
- """Returns information about the running Qdrant instance like version and commit id
- Returns:
- Title, version and optionally commit info
- """
- return self._client.info()
|