Document Stores¤
CouchbaseDocumentStore¤
Base class for Couchbase document stores that provides common functionality for managing connections, scopes, collections, and basic document operations.
Source code in src/couchbase_haystack/document_stores/document_store.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
connection
property
¤
Establishes and returns the Couchbase Cluster connection.
Initializes the connection if it doesn't exist, applying cluster options and authentication. Waits until the cluster is ready before returning.
Returns:
-
Cluster–The active
couchbase.cluster.Clusterinstance.
Raises:
-
DocumentStoreError–If the connection cannot be established or times out.
bucket
property
¤
Returns the Couchbase Bucket object associated with this document store.
Returns:
-
Bucket–The
couchbase.bucket.Bucketinstance.
scope
property
¤
Returns the Couchbase Scope object associated with this document store.
Returns:
-
Scope–The
couchbase.scope.Scopeinstance.
Raises:
-
ValueError–If the specified scope or collection does not exist in the bucket.
collection
property
¤
Returns the Couchbase Collection object associated with this document store.
Returns:
-
Collection–The
couchbase.collection.Collectioninstance.
__init__ ¤
__init__(
*,
cluster_connection_string: Secret = Secret.from_env_var(
"CB_CONNECTION_STRING"
),
authenticator: Union[
CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator
],
cluster_options: CouchbaseClusterOptions = CouchbaseClusterOptions(),
bucket: str,
scope: str,
collection: str,
**kwargs: Dict[str, Any]
)
Parameters:
-
cluster_connection_string(Secret, default:from_env_var('CB_CONNECTION_STRING')) –Connection string for the Couchbase cluster
-
authenticator(Union[CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator]) –Authentication method (password or certificate based)
-
cluster_options(CouchbaseClusterOptions, default:CouchbaseClusterOptions()) –Options for configuring the cluster connection
-
bucket(str) –Name of the Couchbase bucket to use
-
scope(str) –Name of the scope within the bucket
-
collection(str) –Name of the collection within the scope
-
kwargs(Dict[str, Any], default:{}) –Additional keyword arguments passed to the Cluster constructor
Raises:
-
ValueError–If the provided collection name contains invalid characters.
Source code in src/couchbase_haystack/document_stores/document_store.py
_base_to_dict ¤
Creates a base dictionary containing common configuration parameters for serialization.
This is intended to be used by subclasses in their to_dict methods.
Returns:
Source code in src/couchbase_haystack/document_stores/document_store.py
write_documents ¤
Writes documents into the couchbase collection.
Parameters:
-
documents(List[Document]) –A list of Documents to write to the document store.
-
policy(DuplicatePolicy, default:NONE) –The duplicate policy to use when writing documents.
FAIL: (Default ifNONE) Raise an error if a document ID already exists.OVERWRITE: Replace existing documents with the same ID.
Raises:
-
DuplicateDocumentError–If
policyisFAILand a document with the same ID already exists. -
ValueError–If
documentsis not a list ofDocumentobjects. -
DocumentStoreError–If any other error occurs during the write operation.
Returns:
-
int–The number of documents successfully written to the document store.
Source code in src/couchbase_haystack/document_stores/document_store.py
delete_documents ¤
Deletes all documents with a matching document_ids from the document store.
Parameters:
Source code in src/couchbase_haystack/document_stores/document_store.py
CouchbaseSearchDocumentStore¤
Bases: CouchbaseDocumentStore
CouchbaseSearchDocumentStore is a DocumentStore implementation that uses Couchbase capella service that is easy to deploy, operate, and scale.
The document store supports both scope-level and global-level vector search indexes:
- Scope-level indexes (default): The vector search index is created at the scope level and only searches documents within that scope
- Global-level indexes: The vector search index is created at the bucket level and can search across all scopes and collections in the bucket
The index level is specified using the is_global_level_index parameter during initialization.
Source code in src/couchbase_haystack/document_stores/document_store.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
__init__ ¤
__init__(
*,
cluster_connection_string: Secret = Secret.from_env_var(
"CB_CONNECTION_STRING"
),
authenticator: Union[
CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator
],
cluster_options: CouchbaseClusterOptions = CouchbaseClusterOptions(),
bucket: str,
scope: str,
collection: str,
vector_search_index: str,
is_global_level_index: bool = False,
**kwargs: Dict[str, Any]
)
Parameters:
-
cluster_connection_string(Secret, default:from_env_var('CB_CONNECTION_STRING')) –Connection string for the Couchbase cluster
-
authenticator(Union[CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator]) –Authentication method (password or certificate based)
-
cluster_options(CouchbaseClusterOptions, default:CouchbaseClusterOptions()) –Options for configuring the cluster connection
-
bucket(str) –Name of the Couchbase bucket to use
-
scope(str) –Name of the scope within the bucket
-
collection(str) –Name of the collection within the scope
-
vector_search_index(str) –Name of the FTS index (which must include vector indexing) to use for searches.
-
is_global_level_index(bool, default:False) –If
True, use a global (bucket-level) FTS index. IfFalse(default), use a scope-level FTS index. -
kwargs(Dict[str, Any], default:{}) –Additional keyword arguments passed to the Cluster constructor.
Source code in src/couchbase_haystack/document_stores/document_store.py
to_dict ¤
Serializes the component to a dictionary.
Returns:
Source code in src/couchbase_haystack/document_stores/document_store.py
from_dict
classmethod
¤
Deserializes the component from a dictionary.
Parameters:
Returns:
-
CouchbaseSearchDocumentStore–Deserialized component.
Source code in src/couchbase_haystack/document_stores/document_store.py
_get_search_interface ¤
Returns the appropriate Couchbase search interface based on the is_global_level_index configuration.
Returns:
-
–
The Couchbase search index manager object.
Source code in src/couchbase_haystack/document_stores/document_store.py
count_documents ¤
Returns how many documents are present in the document store.
Returns:
-
int–The number of documents in the document store.
Source code in src/couchbase_haystack/document_stores/document_store.py
filter_documents ¤
Returns the documents that match the filters provided.
For a detailed specification of the filters, refer to the Haystack documentation.
Parameters:
-
filters(Optional[Dict[str, Any]], default:None) –The filters to apply. It returns only the documents that match the filters.
Returns:
-
List[Document]–A list of Documents that match the given filters.
Raises:
-
DocumentStoreError–If the search request fails.
Source code in src/couchbase_haystack/document_stores/document_store.py
_embedding_retrieval ¤
_embedding_retrieval(
query_embedding: List[float],
top_k: int = 10,
filters: Optional[Dict[str, Any]] = None,
search_query: SearchQuery = None,
limit: Optional[int] = None,
) -> List[Document]
Find the documents that are most similar to the provided query_embedding by using a vector similarity metric.
Parameters:
-
query_embedding(List[float]) –Embedding of the query
-
top_k(int, default:10) –How many documents to be returned by the vector query
-
filters(Optional[Dict[str, Any]], default:None) –Optional dictionary of filters to apply before the vector search. Refer to Haystack documentation for filter structure (https://docs.haystack.deepset.ai/v2.0/docs/metadata-filtering).
-
search_query(SearchQuery, default:None) –Search filters param which is parsed to the Couchbase search query. The vector query and search query are ORed operation.
-
limit(Optional[int], default:None) –Maximum number of Documents to return. Defaults to top_k if not specified.
Returns:
-
List[Document]–A list of Documents that are most similar to the given
query_embedding
Raises:
-
ValueError–If
query_embeddingis empty -
DocumentStoreError–If the retrieval of documents from Couchbase fails
Source code in src/couchbase_haystack/document_stores/document_store.py
__get_doc_from_kv ¤
Fetches the full document content from Couchbase KV storage based on IDs from a SearchResult.
This helper method takes the results of an FTS/Vector search (which might only contain IDs and scores) and retrieves the complete documents using a multi-get operation for efficiency.
Parameters:
-
response(SearchResult) –The
SearchResultobject containing document IDs and scores.
Returns:
-
List[Document]–A list of Haystack
Documentobjects, populated with content and scores.
Raises:
-
DocumentStoreError–If fetching documents from KV fails for any ID.
Source code in src/couchbase_haystack/document_stores/document_store.py
CouchbaseQueryDocumentStore¤
Bases: CouchbaseDocumentStore
CouchbaseQueryDocumentStore uses Couchbase Global Secondary Index (GSI) for high-performance vector search.
Supports two types of vector indexes:
-
Hyperscale Vector Indexes: Optimized for pure vector searches, scales to billions of documents. Best for chatbot context (RAG), reverse image search, and anomaly detection.
-
Composite Vector Indexes: Combines vector and scalar indexing. Applies scalar filters before vector search. Best for filtered recommendations, job searches, and supply chain management.
Search types: ANN (fast, approximate) or KNN (exact). Similarity metrics: COSINE, DOT, L2/EUCLIDEAN, L2_SQUARED/EUCLIDEAN_SQUARED.
Source code in src/couchbase_haystack/document_stores/document_store.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
__init__ ¤
__init__(
*,
cluster_connection_string: Secret = Secret.from_env_var(
"CB_CONNECTION_STRING"
),
authenticator: Union[
CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator
],
cluster_options: CouchbaseClusterOptions = CouchbaseClusterOptions(),
bucket: str,
scope: str,
collection: str,
search_type: QueryVectorSearchType,
similarity: Union[QueryVectorSearchSimilarity, str],
nprobes: Optional[int] = None,
query_options: CouchbaseQueryOptions = CouchbaseQueryOptions(
timeout=(timedelta(seconds=60)),
scan_consistency=(QueryScanConsistency.NOT_BOUNDED),
),
**kwargs: Dict[str, Any]
)
Parameters:
-
cluster_connection_string(Secret, default:from_env_var('CB_CONNECTION_STRING')) –Connection string for the Couchbase cluster
-
authenticator(Union[CouchbasePasswordAuthenticator, CouchbaseCertificateAuthenticator]) –Authentication method (password or certificate based)
-
cluster_options(CouchbaseClusterOptions, default:CouchbaseClusterOptions()) –Options for configuring the cluster connection
-
bucket(str) –Name of the Couchbase bucket to use
-
scope(str) –Name of the scope within the bucket
-
collection(str) –Name of the collection within the scope
-
search_type(QueryVectorSearchType) –Type of vector search (ANN or KNN).
-
similarity(Union[QueryVectorSearchSimilarity, str]) –Similarity metric to use (COSINE, DOT, L2 or EUCLIDEAN, L2_SQUARED or EUCLIDEAN_SQUARED) or
-
nprobes(Optional[int], default:None) –Number of probes for the ANN search. Defaults to None, uses the value set at index creation time.
-
query_options(CouchbaseQueryOptions, default:CouchbaseQueryOptions(timeout=timedelta(seconds=60), scan_consistency=NOT_BOUNDED)) –Options controlling SQL++ query execution (timeout, scan consistency).
-
kwargs(Dict[str, Any], default:{}) –Additional keyword arguments passed to the
CouchbaseDocumentStorebase class constructor.
Source code in src/couchbase_haystack/document_stores/document_store.py
to_dict ¤
Serializes the component to a dictionary.
Returns:
Source code in src/couchbase_haystack/document_stores/document_store.py
from_dict
classmethod
¤
Deserializes the component from a dictionary.
Parameters:
Returns:
-
CouchbaseQueryDocumentStore–Deserialized component.
Source code in src/couchbase_haystack/document_stores/document_store.py
count_documents ¤
Returns how many documents are present in the document store.
Returns:
-
int–The number of documents in the document store.
Source code in src/couchbase_haystack/document_stores/document_store.py
filter_documents ¤
Returns the documents that match the filters provided.
For a detailed specification of the filters, refer to the Haystack documentation.
Parameters:
-
filters(Optional[Dict[str, Any]], default:None) –The filters to apply using SQL++ WHERE clause syntax. Refer to the Haystack documentation for filter structure.
Returns:
-
List[Document]–A list of Documents that match the given filters.
Raises:
-
DocumentStoreError–If the SQL++ query execution fails.
Source code in src/couchbase_haystack/document_stores/document_store.py
_embedding_retrieval ¤
_embedding_retrieval(
query_embedding: List[float],
top_k: int = 5,
filters: Optional[Dict[str, Any]] = None,
nprobes: Optional[int] = None,
) -> List[Document]
Find the documents that are most similar to the provided query_embedding by using a vector similarity metric.
Parameters:
-
query_embedding(List[float]) –Embedding of the query
-
top_k(int, default:5) –How many documents to retrieve based on vector similarity.
-
filters(Optional[Dict[str, Any]], default:None) –Optional dictionary of filters to apply using a SQL++ WHERE clause before the vector search.
-
nprobes(Optional[int], default:None) –Number of probes for the ANN search. If None, uses the value set at index creation time
Returns:
-
List[Document]–A list of Documents most similar to the
query_embedding, potentially pre-filtered.
Raises:
-
ValueError–If
query_embeddingis empty. -
DocumentStoreError–If the SQL++ query execution fails.
Source code in src/couchbase_haystack/document_stores/document_store.py
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
Supporting Classes¤
QueryVectorSearchType¤
QueryVectorSearchSimilarity¤
Enum for similarity metrics supported by Couchbase GSI.
Source code in src/couchbase_haystack/document_stores/document_store.py
CouchbaseQueryOptions¤
Dataclass for storing query options specifically for Couchbase SQL++ (N1QL) queries.
Parameters:
-
timeout(timedelta, default:timedelta(seconds=60)) –The timeout duration for the query. Defaults to 60 seconds.
-
scan_consistency(Optional[Union[QueryScanConsistency, str]], default:None) –The scan consistency level for the query. See
couchbase.n1ql.QueryScanConsistency. Defaults to None, which implies Couchbase's default behavior.
Source code in src/couchbase_haystack/document_stores/document_store.py
to_dict ¤
Serializes the CouchbaseQueryOptions object to a dictionary.
Returns:
Source code in src/couchbase_haystack/document_stores/document_store.py
from_dict
classmethod
¤
Deserializes a dictionary into a CouchbaseQueryOptions object.
Parameters:
Returns:
-
CouchbaseQueryOptions–A CouchbaseQueryOptions instance.
Source code in src/couchbase_haystack/document_stores/document_store.py
cb_query_options ¤
Returns the underlying Couchbase SDK QueryOptions object.
Returns:
-
QueryOptions–The configured
couchbase.options.QueryOptionsinstance.