Repository: deepset-ai/haystack

Issue:
run parameter "ranking_mode" does not override init param in metafield ranker
**Describe the bug**
When using the metafield ranker and set a different ranking mode in the run method, then choosen when initializing the component, the run parameter does not override the init parameter, as we use [here](https://github.com/deepset-ai/haystack/blob/main/haystack/components/rankers/meta_field.py#L306) the init param, instead of parsing the run param to the _merge_rankings function.

**Expected behavior**
run parameter should always override init param

**To Reproduce**
```from haystack.components.rankers.meta_field import MetaFieldRanker
from haystack import Document

FILES = [
    Document(
        content="Document 1",
        score=0.9,
        meta={
            "feedback": 1,
        },
    ),
    Document(
        content="Document 2",
        score=0.6,
        meta={
            "feedback": 2,
        },
    )
]

ranker = MetaFieldRanker(meta_field="feedback",
                         weight=1,
                         top_k=5,
                         ranking_mode="reciprocal_rank_fusion",
                         sort_order="descending",
                         meta_value_type="int")

results = ranker.run(documents=FILES, ranking_mode="linear_score")
print(results)
```

**FAQ Check**
- [x] Have you had a look at [our new FAQ page](https://docs.haystack.deepset.ai/docs/faq)?


Discussion:
coveralls: ## Pull Request Test Coverage Report for [Build 8319011764](https://coveralls.io/builds/66339556)


### Details

* **0** of **0**   changed or added relevant lines in **0** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage remained the same at **89.204%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/66339556/badge)](https://coveralls.io/builds/66339556) |
| :-- | --: |
| Change from base [Build 8296542501](https://coveralls.io/builds/66315559): |  0.0% |
| Covered Lines: | 5371 |
| Relevant Lines: | 6021 |

---
##### 💛  - [Coveralls](https://coveralls.io)

sjrl: Thanks for making a quick fix on this!

Patch:
diff --git a/haystack/components/rankers/meta_field.py b/haystack/components/rankers/meta_field.py
index a41b8627..d1ba40ba 100644
--- a/haystack/components/rankers/meta_field.py
+++ b/haystack/components/rankers/meta_field.py
@@ -250,7 +250,7 @@ class MetaFieldRanker:
         # Add the docs missing the meta_field back on the end
         sorted_by_meta = [doc for meta, doc in tuple_sorted_by_meta]
         sorted_documents = sorted_by_meta + docs_missing_meta_field
-        sorted_documents = self._merge_rankings(documents, sorted_documents, weight)
+        sorted_documents = self._merge_rankings(documents, sorted_documents, weight, ranking_mode)
         return {"documents": sorted_documents[:top_k]}
 
     def _parse_meta(
@@ -296,18 +296,22 @@ class MetaFieldRanker:
         return meta_values
 
     def _merge_rankings(
-        self, documents: List[Document], sorted_documents: List[Document], weight: float
+        self,
+        documents: List[Document],
+        sorted_documents: List[Document],
+        weight: float,
+        ranking_mode: Literal["reciprocal_rank_fusion", "linear_score"],
     ) -> List[Document]:
         """
         Merge the two different rankings for Documents sorted both by their content and by their meta field.
         """
         scores_map: Dict = defaultdict(int)
 
-        if self.ranking_mode == "reciprocal_rank_fusion":
+        if ranking_mode == "reciprocal_rank_fusion":
             for i, (document, sorted_doc) in enumerate(zip(documents, sorted_documents)):
                 scores_map[document.id] += self._calculate_rrf(rank=i) * (1 - weight)
                 scores_map[sorted_doc.id] += self._calculate_rrf(rank=i) * weight
-        elif self.ranking_mode == "linear_score":
+        elif ranking_mode == "linear_score":
             for i, (document, sorted_doc) in enumerate(zip(documents, sorted_documents)):
                 score = float(0)
                 if document.score is None:
diff --git a/releasenotes/notes/updated-ranking-mode-in-meta-field-ranker-d1f1304cc422b202.yaml b/releasenotes/notes/updated-ranking-mode-in-meta-field-ranker-d1f1304cc422b202.yaml
new file mode 100644
index 00000000..965110ae
--- /dev/null
+++ b/releasenotes/notes/updated-ranking-mode-in-meta-field-ranker-d1f1304cc422b202.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    Fixed a bug in the `MetaFieldRanker`: when the `ranking_mode` parameter was overridden in the `run` method,
+    the component was incorrectly using the `ranking_mode` parameter set in the `__init__` method.


Test patch:
diff --git a/test/components/rankers/test_metafield.py b/test/components/rankers/test_metafield.py
index 9514407e..7462a509 100644
--- a/test/components/rankers/test_metafield.py
+++ b/test/components/rankers/test_metafield.py
@@ -196,7 +196,7 @@ class TestMetaFieldRanker:
         ]
         output = ranker.run(documents=docs_before)
         docs_after = output["documents"]
-        assert docs_after[0].score == 0.01626123744050767
+        assert docs_after[0].score == pytest.approx(0.016261, abs=1e-5)
 
     @pytest.mark.parametrize("score", [-1, 2, 1.3, 2.1])
     def test_linear_score_raises_warning_if_doc_wrong_score(self, score, caplog):
@@ -221,3 +221,18 @@ class TestMetaFieldRanker:
         with caplog.at_level(logging.WARNING):
             ranker.run(documents=docs_before)
             assert "The score wasn't provided; defaulting to 0." in caplog.text
+
+    def test_different_ranking_mode_for_init_vs_run(self):
+        ranker = MetaFieldRanker(meta_field="rating", ranking_mode="linear_score", weight=0.5)
+        docs_before = [
+            Document(content="abc", meta={"rating": 1.3}, score=0.3),
+            Document(content="abc", meta={"rating": 0.7}, score=0.4),
+            Document(content="abc", meta={"rating": 2.1}, score=0.6),
+        ]
+        output = ranker.run(documents=docs_before)
+        docs_after = output["documents"]
+        assert docs_after[0].score == 0.8
+
+        output = ranker.run(documents=docs_before, ranking_mode="reciprocal_rank_fusion")
+        docs_after = output["documents"]
+        assert docs_after[0].score == pytest.approx(0.016261, abs=1e-5)