Indexing Module
The indexing module provides tools for creating custom search indices for various retrieval methods.
Overview
Rankify supports building indices for: - BM25 (Lucene-based sparse retrieval) - DPR (Dense Passage Retrieval) - ANCE (Approximate Nearest Neighbor Negative Contrastive Estimation) - BGE (BAAI General Embedding) - ColBERT (Contextualized Late Interaction over BERT) - Contriever (Contrastive Retriever)
CLI Usage
Use the rankify-index command to build indices:
# BM25 Index
rankify-index index data/corpus.jsonl --retriever bm25 --output ./indices
# Dense Retrievers
rankify-index index data/corpus.jsonl --retriever dpr --device cuda --batch_size 16
# See all options
rankify-index index --help
API Reference
rankify.indexing
__all__ = ['LuceneIndexer', 'DPRIndexer', 'ContrieverIndexer', 'ColBERTIndexer', 'BGEIndexer']
module-attribute
LuceneIndexer
Bases: BaseIndexer
Lucene Indexer for creating and loading Lucene indices using Pyserini. This class handles the preparation of the corpus, building the index, and loading the index.
Source code in rankify/indexing/lucene_indexer.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
build_index()
Build the Lucene index from the corpus.
Source code in rankify/indexing/lucene_indexer.py
load_index()
Load the Lucene index from the specified directory.
This method checks if the index directory exists and is not empty, and raises an error if the index is locked or not found.
Source code in rankify/indexing/lucene_indexer.py
DPRIndexer
Bases: BaseIndexer
DPR Indexer that builds dense FAISS-based indexes using Pyserini.
Supports indexing using models like DPR, ANCE, or BPR via HuggingFace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus_path
|
str
|
Path to the corpus file. |
required |
encoder_name
|
str
|
HuggingFace model name for the DPR encoder. |
'facebook/dpr-ctx_encoder-single-nq-base'
|
output_dir
|
str
|
Directory to save the index. |
'rankify_indices'
|
chunk_size
|
int
|
Size of chunks to process the corpus. |
100
|
threads
|
int
|
Number of threads to use for processing. |
32
|
index_type
|
str
|
Type of index to build (default: "wiki"). |
'wiki'
|
batch_size
|
int
|
Batch size for encoding passages. |
16
|
device
|
str
|
Device to use for encoding ("cpu" or "cuda"). |
'cuda'
|
Source code in rankify/indexing/dpr_indexer.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
build_index()
Build the DPR dense index using FAISS. Steps: 1. Converts corpus to Pyserini JSONL format. 2. Creates ID mappings between FAISS and original document IDs. 3. Runs DPR indexing command using the HuggingFace encoder. 4. Saves all necessary mapping files.
Source code in rankify/indexing/dpr_indexer.py
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 | |
load_index()
Load the DPR index from the specified directory.
This method checks if the index directory exists and is not empty, and raises an error if the index is locked or not found.
Source code in rankify/indexing/dpr_indexer.py
ContrieverIndexer
Bases: BaseIndexer
Corrected Contriever Indexer that efficiently builds dense FAISS-based indexes.
Key improvements: - Memory-efficient batch processing - Proper error handling - Streamlined embedding indexing - Better resource management
Source code in rankify/indexing/contriever_indexer.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
build_index()
Build the Contriever index with improved error handling and efficiency.
Source code in rankify/indexing/contriever_indexer.py
load_index()
Load the Contriever index from the specified directory.
Source code in rankify/indexing/contriever_indexer.py
validate_index()
Validate that the index was built correctly.
Source code in rankify/indexing/contriever_indexer.py
get_index_stats()
Get statistics about the built index.
Source code in rankify/indexing/contriever_indexer.py
ColBERTIndexer
Bases: BaseIndexer
ColBERT Indexer that properly handles string IDs by converting them to sequential integers.
This indexer: 1. Converts string IDs to sequential integers (required by ColBERT) 2. Maintains ID mappings for retrieval 3. Creates proper TSV header format 4. Generates all necessary files for the retriever
Source code in rankify/indexing/colbert_indexer.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 350 351 352 353 354 355 356 357 358 359 360 | |
build_index()
Build the ColBERT index with proper string ID handling.
Source code in rankify/indexing/colbert_indexer.py
load_index()
Load the ColBERT index.
Source code in rankify/indexing/colbert_indexer.py
load_id_mappings()
Load ID mappings for retrieval.
Source code in rankify/indexing/colbert_indexer.py
get_original_id(sequential_id)
Convert sequential ID back to original ID.
get_sequential_id(original_id)
Convert original ID to sequential ID.
BGEIndexer
Bases: BaseIndexer
Corrected BGE Indexer that builds dense FAISS-based indexes with proper normalization.
Key improvements: - L2 normalization of embeddings - Proper CLS token extraction for BGE models - Cosine similarity via normalized embeddings - Better error handling and validation
Source code in rankify/indexing/bge_indexer.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 | |
build_faiss_index()
Build FAISS index with normalized embeddings for cosine similarity.
Source code in rankify/indexing/bge_indexer.py
build_index()
Build the BGE index from the corpus.
Source code in rankify/indexing/bge_indexer.py
load_index()
Load the FAISS index and document IDs from the index directory.