# reciprocal_rank_fusion

> **Function** in `langchain_postgres`

📖 [View in docs](https://reference.langchain.com/python/langchain-postgres/v2/hybrid_search_config/reciprocal_rank_fusion)

Ranks documents using Reciprocal Rank Fusion (RRF) of scores from two sources.

## Signature

```python
reciprocal_rank_fusion(
    primary_search_results: Sequence[RowMapping],
    secondary_search_results: Sequence[RowMapping],
    rrf_k: float = 60,
    fetch_top_k: int = 4,
    **kwargs: Any = {},
) -> Sequence[dict[str, Any]]
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `primary_search_results` | `Sequence[RowMapping]` | Yes | A list of (document, distance) tuples from the primary search. |
| `secondary_search_results` | `Sequence[RowMapping]` | Yes | A list of (document, distance) tuples from the secondary search. |
| `rrf_k` | `float` | No | The RRF parameter k. Defaults to 60. (default: `60`) |
| `fetch_top_k` | `int` | No | The number of documents to fetch after merging the results. Defaults to 4. (default: `4`) |

## Returns

`Sequence[dict[str, Any]]`

A list of (document_id, rrf_score) tuples, sorted by rrf_score

---

[View source on GitHub](https://github.com/langchain-ai/langchain-postgres/blob/9d819182da7e3fb1b815e6c9a6b7c2cd1eb4bc0e/langchain_postgres/v2/hybrid_search_config.py#L119)