Batch sparse search using cosdata client
(collection, batch, top_k=10, early_terminate_threshold=0.0)
| 108 | |
| 109 | |
| 110 | def batch_ann_search(collection, batch, top_k=10, early_terminate_threshold=0.0): |
| 111 | """Batch sparse search using cosdata client""" |
| 112 | try: |
| 113 | results = collection.search.batch_sparse( |
| 114 | query_terms_list=batch, |
| 115 | top_k=top_k, |
| 116 | early_terminate_threshold=early_terminate_threshold, |
| 117 | ) |
| 118 | return results |
| 119 | except Exception as e: |
| 120 | print(f"Error in batch search: {e}") |
| 121 | # Fallback to individual searches |
| 122 | results = [] |
| 123 | for query_terms in batch: |
| 124 | try: |
| 125 | result = collection.search.sparse( |
| 126 | query_terms=query_terms, |
| 127 | top_k=top_k, |
| 128 | early_terminate_threshold=early_terminate_threshold, |
| 129 | ) |
| 130 | results.append(result) |
| 131 | except Exception as e2: |
| 132 | print(f"Individual search failed: {e2}") |
| 133 | results.append({"results": []}) |
| 134 | return results |
| 135 | |
| 136 | |
| 137 | def generate_random_sparse_vector(id, dimension, non_zero_dims): |