MCPcopy Create free account
hub / github.com/alibaba/zvec / Query

Class Query

python/zvec/model/param/query.py:43–130  ·  view source on GitHub ↗

Represents a search query for a specific field in a collection. A `Query` can be constructed for either vector search or full-text search, but not both simultaneously. For vector search, provide `id` or `vector` (and optionally `param`). For FTS, provide `fts`. Attributes:

Source from the content-addressed store, hash-verified

41
42@dataclass(frozen=True)
43class Query:
44 """Represents a search query for a specific field in a collection.
45
46 A `Query` can be constructed for either vector search or full-text search,
47 but not both simultaneously.
48
49 For vector search, provide `id` or `vector` (and optionally `param`).
50 For FTS, provide `fts`.
51
52 Attributes:
53 field_name (str): Name of the field to query.
54 id (Optional[str], optional): Document ID to fetch vector from. Default is None.
55 vector (VectorType, optional): Explicit query vector. Default is None.
56 param (Optional[Union[HnswQueryParam, HnswRabitqQueryParam, IVFQueryParam, FtsQueryParam]], optional):
57 Index-specific query parameters. Default is None.
58 fts (Optional[Fts], optional): Full-text search parameters. Default is None.
59
60 Examples:
61 >>> import zvec
62 >>> # Query by ID
63 >>> q1 = zvec.Query(field_name="embedding", id="doc123")
64 >>> # Query by vector
65 >>> q2 = zvec.Query(
66 ... field_name="embedding",
67 ... vector=[0.1, 0.2, 0.3],
68 ... param=HnswQueryParam(ef=300)
69 ... )
70 >>> # FTS query
71 >>> q3 = zvec.Query(
72 ... field_name="content",
73 ... fts=Fts(match_string="machine learning")
74 ... )
75 >>> # FTS query with custom operator
76 >>> q4 = zvec.Query(
77 ... field_name="content",
78 ... fts=Fts(match_string="machine learning"),
79 ... param=FtsQueryParam(default_operator="AND")
80 ... )
81 """
82
83 field_name: str
84 id: Optional[str] = None
85 vector: VectorType = None
86 param: Optional[
87 Union[HnswQueryParam, HnswRabitqQueryParam, IVFQueryParam, FtsQueryParam]
88 ] = None
89 fts: Optional[Fts] = None
90
91 def has_id(self) -> bool:
92 """Check if the query is based on a document ID.
93
94 Returns:
95 bool: True if `id` is set, False otherwise.
96 """
97 return self.id is not None
98
99 def has_vector(self) -> bool:
100 """Check if the query contains an explicit vector.

Calls

no outgoing calls