Create or recreate the collection with proper configuration
(name: str)
| 720 | return results |
| 721 | |
| 722 | def ensure_collection(name: str): |
| 723 | """Create or recreate the collection with proper configuration""" |
| 724 | try: |
| 725 | print(f"Creating collection {name}...") |
| 726 | collection = client.create_collection( |
| 727 | name=name, |
| 728 | dimension=DIMENSION, |
| 729 | tf_idf_options={"enabled": True}, |
| 730 | ) |
| 731 | print("Collection created successfully") |
| 732 | |
| 733 | print("Creating dense HNSW index...") |
| 734 | collection.create_index(distance_metric="cosine") |
| 735 | |
| 736 | print("Creating TF-IDF index...") |
| 737 | collection.create_tf_idf_index(name=name, k1=K1, b=B) |
| 738 | |
| 739 | # Add hybrid search methods to collection via monkey patching |
| 740 | collection.search.hybrid = hybrid.__get__(collection.search) |
| 741 | collection.search.batch_hybrid = batch_hybrid.__get__(collection.search) |
| 742 | |
| 743 | return collection |
| 744 | |
| 745 | except Exception as e: |
| 746 | print( |
| 747 | f"Collection {name} may already exist, attempting to delete and recreate: {e}" |
| 748 | ) |
| 749 | try: |
| 750 | existing = client.get_collection(name) |
| 751 | existing.delete() |
| 752 | time.sleep(2) |
| 753 | except Exception as delete_error: |
| 754 | print(f"Error deleting collection: {delete_error}") |
| 755 | |
| 756 | collection = client.create_collection( |
| 757 | name=name, dimension=DIMENSION, tf_idf_options={"enabled": True} |
| 758 | ) |
| 759 | collection.create_index(distance_metric="cosine") |
| 760 | collection.create_tf_idf_index(name=name, k1=K1, b=B) |
| 761 | |
| 762 | # Add hybrid search methods to collection via monkey patching |
| 763 | collection.search.hybrid = hybrid.__get__(collection.search) |
| 764 | collection.search.batch_hybrid = batch_hybrid.__get__(collection.search) |
| 765 | |
| 766 | return collection |
| 767 | |
| 768 | def upsert_hybrid_vectors(collection, corpus_ids, corpus_embeddings, corpus): |
| 769 | """Upsert both dense embeddings and raw text for hybrid search""" |
no test coverage detected