Repository: deepset-ai/haystack

Issue:
`MetaFieldRanker` - the `weight` parameter passed in the `run` method is not taken into account
**Describe the bug**
When passing the `weight` in the `run` method, it is not taken into account.
Only `self.weight` (defined during initialization) is used.

**To Reproduce**
```python
from haystack import Document
from haystack.components.rankers import MetaFieldRanker

ranker = MetaFieldRanker(meta_field="rating", weight=0) # weight=0 disables ranking by a meta field.
docs = [
    Document(content="Paris", meta={"rating": 1}),
    Document(content="Berlin", meta={"rating": 0.5}),
    Document(content="Barcelona", meta={"rating": 5}),
]

output = ranker.run(documents=docs, weight=1) # weight=1 means ranking by a meta field only

print(output)
# we get Documents with the original order, weight=1 is not taken into account
```

**System:**
 - Haystack version (commit or version number): 2.0-beta8



Discussion:

Resolving patch:
diff --git a/haystack/components/rankers/meta_field.py b/haystack/components/rankers/meta_field.py
--- a/haystack/components/rankers/meta_field.py
+++ b/haystack/components/rankers/meta_field.py
@@ -96,7 +96,7 @@ def _validate_params(
                 "Parameter <weight> must be in range [0,1] but is currently set to '%s'.\n'0' disables sorting by a "
                 "meta field, '0.5' assigns equal weight to the previous relevance scores and the meta field, and "
                 "'1' ranks by the meta field only.\nChange the <weight> parameter to a value in range 0 to 1 when "
-                "initializing the MetaFieldRanker." % self.weight
+                "initializing the MetaFieldRanker." % weight
             )
 
         if ranking_mode not in ["reciprocal_rank_fusion", "linear_score"]:
@@ -239,7 +239,7 @@ def run(
         # 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)
+        sorted_documents = self._merge_rankings(documents, sorted_documents, weight)
         return {"documents": sorted_documents[:top_k]}
 
     def _parse_meta(
@@ -284,7 +284,9 @@ def _parse_meta(
 
         return meta_values
 
-    def _merge_rankings(self, documents: List[Document], sorted_documents: List[Document]) -> List[Document]:
+    def _merge_rankings(
+        self, documents: List[Document], sorted_documents: List[Document], weight: float
+    ) -> List[Document]:
         """
         Merge the two different rankings for Documents sorted both by their content and by their meta field.
         """
@@ -292,8 +294,8 @@ def _merge_rankings(self, documents: List[Document], sorted_documents: List[Docu
 
         if self.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 - self.weight)
-                scores_map[sorted_doc.id] += self._calculate_rrf(rank=i) * self.weight
+                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":
             for i, (document, sorted_doc) in enumerate(zip(documents, sorted_documents)):
                 score = float(0)
@@ -308,8 +310,8 @@ def _merge_rankings(self, documents: List[Document], sorted_documents: List[Docu
                 else:
                     score = document.score
 
-                scores_map[document.id] += score * (1 - self.weight)
-                scores_map[sorted_doc.id] += self._calc_linear_score(rank=i, amount=len(sorted_documents)) * self.weight
+                scores_map[document.id] += score * (1 - weight)
+                scores_map[sorted_doc.id] += self._calc_linear_score(rank=i, amount=len(sorted_documents)) * weight
 
         for document in documents:
             document.score = scores_map[document.id]


Test patch:
diff --git a/test/components/rankers/test_metafield.py b/test/components/rankers/test_metafield.py
--- a/test/components/rankers/test_metafield.py
+++ b/test/components/rankers/test_metafield.py
@@ -1,6 +1,7 @@
-import pytest
 import logging
 
+import pytest
+
 from haystack import Document
 from haystack.components.rankers.meta_field import MetaFieldRanker
 
@@ -79,6 +80,16 @@ def test_run_with_weight_equal_to_1(self):
         sorted_scores = sorted([doc.meta["rating"] for doc in docs_after], reverse=True)
         assert [doc.meta["rating"] for doc in docs_after] == sorted_scores
 
+    def test_run_with_weight_equal_to_1_passed_in_run_method(self):
+        ranker = MetaFieldRanker(meta_field="rating", weight=0.0)
+        docs_before = [Document(content="abc", meta={"rating": value}) for value in [1.1, 0.5, 2.3]]
+        output = ranker.run(documents=docs_before, weight=1.0)
+        docs_after = output["documents"]
+
+        assert len(docs_after) == 3
+        sorted_scores = sorted([doc.meta["rating"] for doc in docs_after], reverse=True)
+        assert [doc.meta["rating"] for doc in docs_after] == sorted_scores
+
     def test_sort_order_ascending(self):
         ranker = MetaFieldRanker(meta_field="rating", weight=1.0, sort_order="ascending")
         docs_before = [Document(content="abc", meta={"rating": value}) for value in [1.1, 0.5, 2.3]]