Repository: deepset-ai/haystack

Issue:
Add Distribution-based rank fusion in JoinDocuments
**Is your feature request related to a problem? Please describe.**
Add [Distribution-based rank fusion](https://medium.com/plain-simple-software/distribution-based-score-fusion-dbsf-a-new-approach-to-vector-search-ranking-f87c37488b18) in JoinDocuments

**Describe the solution you'd like**
```
def _distribution_based_rank_fusion(self, document_lists):
        """
        Merge multiple lists of Documents and assign scores based on Distribution-Based Score Fusion.
        (https://medium.com/plain-simple-software/distribution-based-score-fusion-dbsf-a-new-approach-to-vector-search-ranking-f87c37488b18)

        If a Document is in more than one retriever, the sone with the highest score is used.
        """
        for documents in document_lists:
            scores_list = []

            for doc in documents:
                scores_list.append(doc.score)

            mean_score = sum(scores_list) / len(scores_list)
            std_dev = (
                sum((x - mean_score) ** 2 for x in scores_list) / len(scores_list)
            ) ** 0.5
            min_score = mean_score - 3 * std_dev
            max_score = mean_score + 3 * std_dev

            for doc in documents:
                doc.score = (doc.score - min_score) / (max_score - min_score)

        output = self._concatenate(document_lists=document_lists)

        return output
```


Discussion:

Resolving patch:
diff --git a/haystack/components/joiners/document_joiner.py b/haystack/components/joiners/document_joiner.py
--- a/haystack/components/joiners/document_joiner.py
+++ b/haystack/components/joiners/document_joiner.py
@@ -195,9 +195,11 @@ def _distribution_based_rank_fusion(self, document_lists: List[List[Document]])
             std_dev = (sum((x - mean_score) ** 2 for x in scores_list) / len(scores_list)) ** 0.5
             min_score = mean_score - 3 * std_dev
             max_score = mean_score + 3 * std_dev
+            delta_score = max_score - min_score
 
             for doc in documents:
-                doc.score = (doc.score - min_score) / (max_score - min_score)
+                doc.score = (doc.score - min_score) / delta_score if delta_score != 0.0 else 0.0
+                # if all docs have the same score delta_score is 0, the docs are uninformative for the query
 
         output = self._concatenate(document_lists=document_lists)
 


Test patch:
diff --git a/test/components/joiners/test_document_joiner.py b/test/components/joiners/test_document_joiner.py
--- a/test/components/joiners/test_document_joiner.py
+++ b/test/components/joiners/test_document_joiner.py
@@ -145,6 +145,36 @@ def test_run_with_distribution_based_rank_fusion_join_mode(self):
         ]
         assert all(doc.id in expected_document_ids for doc in output["documents"])
 
+    def test_run_with_distribution_based_rank_fusion_join_mode_same_scores(self):
+        joiner = DocumentJoiner(join_mode="distribution_based_rank_fusion")
+        documents_1 = [
+            Document(content="a", score=0.2),
+            Document(content="b", score=0.2),
+            Document(content="c", score=0.2),
+        ]
+        documents_2 = [
+            Document(content="d", score=0.5),
+            Document(content="e", score=0.8),
+            Document(content="f", score=1.1, meta={"key": "value"}),
+            Document(content="g", score=0.3),
+            Document(content="a", score=0.3),
+        ]
+        output = joiner.run([documents_1, documents_2])
+        assert len(output["documents"]) == 7
+        expected_document_ids = [
+            doc.id
+            for doc in [
+                Document(content="a", score=0),
+                Document(content="b", score=0),
+                Document(content="c", score=0),
+                Document(content="d", score=0.44),
+                Document(content="e", score=0.60),
+                Document(content="f", score=0.76, meta={"key": "value"}),
+                Document(content="g", score=0.33),
+            ]
+        ]
+        assert all(doc.id in expected_document_ids for doc in output["documents"])
+
     def test_run_with_top_k_in_run_method(self):
         joiner = DocumentJoiner()
         documents_1 = [Document(content="a"), Document(content="b"), Document(content="c")]