# What is the process to use a cross-encoder from the Sentence Transformers library for re-ranking search results?

Source: https://zilliz.com/ai-faq/what-is-the-process-to-use-a-crossencoder-from-the-sentence-transformers-library-for-reranking-search-results
Author: zilliz

To use a cross-encoder from the Sentence Transformers library for re-ranking search results, follow these steps. First, perform an initial retrieval of documents using a fast but less accurate method (like BM25 or a bi-encoder model). This step narrows down thousands of potential documents to a manageable subset (e.g., 100-200 candidates). Cross-encoders are computationally expensive because they process query-document pairs jointly, so applying them to a smaller candidate pool balances accuracy and efficiency. Once you have the initial results, pass each query-candidate pair through the cross-encoder to compute a relevance score. These scores are used to reorder the candidates, placing the most relevant documents at the top.

The technical implementation involves installing the `sentence-transformers` library and loading a pre-trained cross-encoder model. For example, `CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')` initializes a model fine-tuned for ranking. Prepare your data by pairing the query with each candidate document (e.g., `pairs = [(query, doc1), (query, doc2), ...]`). Use the model’s `predict()` method to generate similarity scores for these pairs. The scores represent how well each document matches the query. Since cross-encoders output raw logits or probabilities, you might need to apply a sigmoid function depending on the model’s training objective. Finally, sort the documents based on these scores in descending order to produce the re-ranked list.

Key considerations include choosing the right model for your domain (e.g., MS MARCO models for web search) and limiting the candidate pool size to avoid latency. Cross-encoders improve ranking quality by capturing nuanced interactions between queries and documents, unlike bi-encoders that compute embeddings separately. However, they’re impractical for large-scale retrieval due to their O(n) computational complexity per query. By combining a fast first-stage retriever with a cross-encoder reranker, you achieve a balance between speed and accuracy, which is critical in production systems.
